|
12 | 12 |
|
13 | 13 |
|
14 | 14 | class PynamodbPlugin(Plugin): |
15 | | - @staticmethod |
16 | | - def _get_attribute_underlying_type(attribute_class: TypeInfo) -> Optional[mypy.types.Type]: |
17 | | - """ |
18 | | - e.g. for `class MyAttribute(Attribute[int])`, this will return `int`. |
19 | | - """ |
20 | | - for base_instance in attribute_class.bases: |
21 | | - if base_instance.type.fullname() == ATTR_FULL_NAME: |
22 | | - return base_instance.args[0] |
23 | | - return None |
24 | | - |
25 | 15 | def get_function_hook(self, fullname: str) -> Optional[Callable[[FunctionContext], mypy.types.Type]]: |
26 | | - symbol_table_node = self.lookup_fully_qualified(fullname) |
27 | | - if not symbol_table_node: |
28 | | - return None |
29 | | - |
30 | | - if isinstance(symbol_table_node.node, TypeInfo): |
31 | | - underlying_type = self._get_attribute_underlying_type(symbol_table_node.node) |
32 | | - if underlying_type: |
33 | | - _underlying_type = underlying_type # https://github.com/python/mypy/issues/4297 |
| 16 | + sym = self.lookup_fully_qualified(fullname) |
| 17 | + if sym and isinstance(sym.node, TypeInfo): |
| 18 | + attr_underlying_type = _get_attribute_underlying_type(sym.node) |
| 19 | + if attr_underlying_type: |
| 20 | + _underlying_type = attr_underlying_type # https://github.com/python/mypy/issues/4297 |
34 | 21 | return lambda ctx: _attribute_instantiation_hook(ctx, _underlying_type) |
35 | 22 |
|
36 | 23 | return None |
37 | 24 |
|
38 | 25 |
|
| 26 | +def _get_attribute_underlying_type(attribute_class: TypeInfo) -> Optional[mypy.types.Type]: |
| 27 | + """ |
| 28 | + For attribute classes, will return the underlying type. |
| 29 | + e.g. for `class MyAttribute(Attribute[int])`, this will return `int`. |
| 30 | + """ |
| 31 | + for base_instance in attribute_class.bases: |
| 32 | + if base_instance.type.fullname() == ATTR_FULL_NAME: |
| 33 | + return base_instance.args[0] |
| 34 | + return None |
| 35 | + |
| 36 | + |
39 | 37 | def _attribute_instantiation_hook(ctx: FunctionContext, |
40 | 38 | underlying_type: mypy.types.Type) -> mypy.types.Type: |
41 | 39 | """ |
|
0 commit comments