Skip to content

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