Skip to content

Commit ad05f3c

Browse files
authored
Move _get_attribute_underlying_type outside of Plugin class (#4)
1 parent 77b1a81 commit ad05f3c

File tree

1 file changed

+16
-18
lines changed

1 file changed

+16
-18
lines changed

pynamodb_mypy/plugin.py

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,30 +12,28 @@
1212

1313

1414
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-
2515
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
3421
return lambda ctx: _attribute_instantiation_hook(ctx, _underlying_type)
3522

3623
return None
3724

3825

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+
3937
def _attribute_instantiation_hook(ctx: FunctionContext,
4038
underlying_type: mypy.types.Type) -> mypy.types.Type:
4139
"""

0 commit comments

Comments
 (0)