-
-
Notifications
You must be signed in to change notification settings - Fork 32k
inspect.getsource
(findsource
) not working as expected when duplicate class is used
#106727
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
Comments
inspect.getsource
not working as expected when duplicate class is usedinspect.getsource
(findsource
) not working as expected when duplicate class is used
The method has already been changed from regex to ast. I can work on the PR if you are okay with it. If you want to fix this yourself, the code piece is at the similar place, just switched to an |
Just BTW, the last class may not be the one you need. if cond:
class A:
...
else:
class A:
... We could probably need some heuristics to figure out which class it should be. |
Yes by look at what was done in #10307 I meant improve this ast based search.
I wonder if this is possible depending of the condition evaluated. |
Oh sorry I did not look at the PR thoroughly. I saw the word "regex" and thought it was improving the regex method. classes do not have useful compile time information with it as far as I know which makes this tricky - that's why I said we probably need some heuristics. I doubt if we can make this work: if runtime_cond:
class A:
pass
else:
class A:
pass They are indistinguishable. We can, however, try to figure out which class it is by some "guessing" like reading their methods. Let me know if this is out of your comfortable zone and you'd like me to work on it, I'm kind of familiar with the area. Otherwise, let me know if you have any question about the implementation. |
I'll try to take a look, I'm not that familiar with |
Actually while I see how this could be implemented, I don't really see which attributes should be used to guess the right class. @gaogaotiantian if you have something in mind I'll be pleased to see a PR |
No worries, I'll work on it, it can be a bit tricky. |
Yeah I was first thinking maybe use the values from |
Bug report
Consider the following example:
I think we should expect the last class to be used (it works as expected if we use functions instead of classes).
This issue was raised in an external project:
And I'm facing this issue while working on:
FieldInfo.description
pydantic/pydantic#6563Let me know if this should be fixed, I'll take a deeper look at what was done in #10307 and see if I can make a PR
Your environment
Linked PRs
inspect.getsource
smarter for class for same name definitions #106815__module__
check forinspect.getsource(cls)
#106968The text was updated successfully, but these errors were encountered: