Skip to content

Commit

Permalink
Fix ClassDef insert
Browse files Browse the repository at this point in the history
  • Loading branch information
cdce8p committed Feb 28, 2021
1 parent 4e3cf53 commit 2391061
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 17 deletions.
12 changes: 6 additions & 6 deletions astroid/brain/brain_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,13 +178,13 @@ def infer_typing_alias(

if res != astroid.Uninferable and isinstance(res, nodes.ClassDef):
class_def = nodes.ClassDef(
name=res.name,
lineno=res.lineno,
col_offset=res.col_offset,
name=f"{res.name}_typing",
lineno=0,
col_offset=0,
parent=res.parent,
)
class_def.postinit(
bases=res.bases,
bases=[res],
body=res.body,
decorators=res.decorators,
metaclass=create_typing_metaclass(),
Expand All @@ -194,8 +194,8 @@ def infer_typing_alias(
if len(node.args) == 2 and isinstance(node.args[0], nodes.Attribute):
class_def = nodes.ClassDef(
name=node.args[0].attrname,
lineno=node.lineno,
col_offset=node.col_offset,
lineno=0,
col_offset=0,
parent=node.parent,
)
class_def.postinit(
Expand Down
21 changes: 10 additions & 11 deletions tests/unittest_brain.py
Original file line number Diff line number Diff line change
Expand Up @@ -1218,18 +1218,15 @@ def test_typing_alias_type(self):

def check_metaclass(node: nodes.ClassDef):
meta = node.metaclass()
assert (
isinstance(meta, nodes.ClassDef)
and meta.name == "ABCMeta_typing"
and "ABCMeta" == meta.basenames[0]
and meta.locals.get("__getitem__") is not None
)
assert isinstance(meta, nodes.ClassDef)
assert meta.name == "ABCMeta_typing"
assert "ABCMeta" == meta.basenames[0]
assert meta.locals.get("__getitem__") is not None

abc_meta = next(meta.bases[0].infer())
assert (
isinstance(abc_meta, nodes.ClassDef)
and abc_meta.name == "ABCMeta"
and abc_meta.locals.get("__getitem__") is None
)
assert isinstance(abc_meta, nodes.ClassDef)
assert abc_meta.name == "ABCMeta"
assert abc_meta.locals.get("__getitem__") is None

node = builder.extract_node(
"""
Expand All @@ -1248,6 +1245,7 @@ class Derived1(MutableSet[T]):
inferred,
[
"Derived1",
"MutableSet_typing",
"MutableSet",
"Set",
"Collection",
Expand All @@ -1271,6 +1269,7 @@ class Derived2(typing.OrderedDict[int, str]):
inferred,
[
"Derived2",
"OrderedDict_typing",
"OrderedDict",
"dict",
"object",
Expand Down

0 comments on commit 2391061

Please sign in to comment.