Skip to content

gh-106727: Add __module__ check for inspect.getsource(cls) #106968

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

Merged
merged 3 commits into from
Jul 24, 2023

Conversation

gaogaotiantian
Copy link
Member

@gaogaotiantian gaogaotiantian commented Jul 21, 2023

In method heuristics, make sure the method we check against comes from the same file as the class. Fixed the issue from #106815

@gaogaotiantian
Copy link
Member Author

This fix is straightforward enough and is a supplement to #106815, so I skipped the news. If @carljm thinks the other way, I can add the news. Also do we want a test for it? It requires some delicate file line number conflict from two files.

Copy link
Member

@carljm carljm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a test that fails without this fix?

This doesn't fix the case I mentioned (metaclass and class using it, both with the same name and in the same module), but it does fix the case where the metaclass is in a different module and the line numbers just happen to collide with another class of the same name.

I'm not seeing a clear way to fix the "metaclass with same name" case, and I'm also not really worried about it.

@bedevere-bot
Copy link

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@carljm
Copy link
Member

carljm commented Jul 21, 2023

This fix is straightforward enough and is a supplement to #106815, so I skipped the news. If @carljm thinks the other way, I can add the news. Also do we want a test for it? It requires some delicate file line number conflict from two files.

Oops, this comment crossed with my review. I think skipping news is fine, but I think we should have a test, even though it requires additional files. Subtle cases like this will break again in future refactors if there's no test. If it's worth fixing, it's worth testing.

@gaogaotiantian gaogaotiantian requested a review from carljm July 22, 2023 18:47
@gaogaotiantian
Copy link
Member Author

Fixed the style and added the test case - used a temporary directory for the files so I don't need to add extra files to the source code. Without the new check, the code would've found the first class as the line number of method borrowed from the other module happens to be inside the first class defined in inspect_actual.

Confirmed that the test would fail without the new check.

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

Successfully merging this pull request may close these issues.

3 participants