Skip to content

Skip more method bodies in third-party libraries #19586

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 1 commit into from
Aug 5, 2025

Conversation

ilevkivskyi
Copy link
Member

A while ago we started stripping function bodies when checking third-party libraries. This PR pushes this idea further:

  • Tighten the check in fastparse.py to only consider foo.bar as possible self attribute definition.
  • Do not type-check bodies where we didn't find any self attribute definitions during semantic analysis.
  • Skip method override checks in third-party libraries.

In total this makes e.g. mypy -c 'import torch' ~10% faster. Surprisingly, this also has some visible impact on self-check.

Copy link
Contributor

github-actions bot commented Aug 4, 2025

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

Copy link
Collaborator

@sterliakov sterliakov left a comment

Choose a reason for hiding this comment

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

Makes sense! I'm surprised by visible selfcheck improvement, though: if there is any, maybe something is wrong, mypy doesn't have a lot of non-stub dependencies... One question (I'm not very familiar with that part, sorry it it's dumb): can this change cause any issues with PEP695 variance inference? Probably not, all self attribute assignments should be retained here, but I'd better ask

@@ -856,6 +857,8 @@ def __init__(
# the majority). In cases where self is not annotated and there are no Self
# in the signature we can simply drop the first argument.
self.is_trivial_self = False
# Keep track of functions where self attributes are defined.
self.has_self_attr_def = False
Copy link
Collaborator

Choose a reason for hiding this comment

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

Wow, I love this. Can we use this flag later to fix overly optimistic narrowing like in #17537? (even just "this method has an explicit assignment to self" would be better than nothing, but even tracking nested method calls should be doable)

Copy link
Member Author

Choose a reason for hiding this comment

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

FWIW we can, but with great care (it is one thing to do something as an optimization, and another thing to do the same for correctness).

Copy link
Collaborator

@hauntsaninja hauntsaninja left a comment

Choose a reason for hiding this comment

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

Nice!

re self check: naively my guess would just be pytest

@ilevkivskyi
Copy link
Member Author

@sterliakov

can this change cause any issues with PEP695 variance inference?

I think no. (also we are already doing this in a lot of cases, this PR simply increases the number of scenarios where we can do this)

@ilevkivskyi ilevkivskyi merged commit 83fed5a into python:master Aug 5, 2025
20 checks passed
@ilevkivskyi ilevkivskyi deleted the more-skip-body branch August 5, 2025 13:26
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.

3 participants