Skip to content

Commit

Permalink
Fix bug with multiple generic inheritence
Browse files Browse the repository at this point in the history
Previously if the same generic base class appeared multiple times in a
classes inheritence graph msgspec would error when trying to analyze the
class. This PR fixes the bug and adds a test for the behavior.
  • Loading branch information
jcrist committed Jan 5, 2024
1 parent fc75a28 commit a5674c1
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
11 changes: 5 additions & 6 deletions msgspec/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,12 @@ def inner(c, scope):
new_scope = {}
else:
cls = getattr(c, "__origin__", None)
if cls in (None, object, typing.Generic):
if cls in (None, object, typing.Generic) or cls in mapping:
return
if cls not in mapping:
params = cls.__parameters__
args = tuple(_apply_params(a, scope) for a in c.__args__)
assert len(params) == len(args)
mapping[cls] = new_scope = dict(zip(params, args))
params = cls.__parameters__
args = tuple(_apply_params(a, scope) for a in c.__args__)
assert len(params) == len(args)
mapping[cls] = new_scope = dict(zip(params, args))

if issubclass(cls, typing.Generic):
bases = getattr(cls, "__orig_bases__", cls.__bases__)
Expand Down
9 changes: 9 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,3 +181,12 @@ class Sub3(Sub[List[T]]):
"a": List[List[int]],
"b": List[int],
}

def test_generic_sub11(self):
class Sub(Base[int]):
y: float

class Sub2(Sub, Base[int]):
z: str

assert get_class_annotations(Sub2) == {"x": int, "y": float, "z": str}

0 comments on commit a5674c1

Please sign in to comment.