Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes class disambiguation logic when there is nested inheritance #170

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

aasiffaizal
Copy link

@aasiffaizal aasiffaizal commented Dec 2, 2024

Fixes the case mentioned here in #117

Below is the example pointed out by @slafs

# -- bar.py
from package.foo import Foo

class Bar(Foo):
    b: str
# -- foo.py
from pydantic import BaseModel

class Foo(BaseModel):
    a: str
# -- baz.py
from package.bar import Bar

class Baz(Bar):
    c: str

At the end class visits we are left with CLS_CONTEXT_KEY being

{'package.bar.Bar': {'package.baz.Baz'}}

Instead of just disambiguating at one level over here

# In case we have the following scenario:
# class A(B): ...
# class B(BaseModel): ...
# class D(C): ...
# class C: ...
# We want to disambiguate `A` as soon as we see `B` is a `BaseModel`.
if (
fqn.name in self.context.scratch[self.BASE_MODEL_CONTEXT_KEY]
and fqn.name in self.context.scratch[self.CLS_CONTEXT_KEY]
):
for parent_class in self.context.scratch[self.CLS_CONTEXT_KEY].pop(fqn.name):
self.context.scratch[self.BASE_MODEL_CONTEXT_KEY].add(parent_class)
# In case we have the following scenario:
# class A(B): ...
# class B(BaseModel): ...
# class D(C): ...
# class C: ...
# We want to disambiguate `D` as soon as we see `C` is NOT a `BaseModel`.
if (
fqn.name in self.context.scratch[self.NO_BASE_MODEL_CONTEXT_KEY]
and fqn.name in self.context.scratch[self.CLS_CONTEXT_KEY]
):
for parent_class in self.context.scratch[self.CLS_CONTEXT_KEY].pop(fqn.name):
self.context.scratch[self.NO_BASE_MODEL_CONTEXT_KEY].add(parent_class)

I have changed the logic to recursively disambiguate its children as well.

I noticed that ClassDefVisitor unittest is fully commented out so I added an integration test for it. Happy to add the unittest if required.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant