FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Skip more method bodies in third-party libraries by ilevkivskyi · Pull Request #19586 · python/mypy · GitHub

Repository navigation

Skip more method bodies in third-party libraries - #19586

Merged
ilevkivskyi merged 1 commit into
python:masterfrom
ilevkivskyi:more-skip-body
Aug 5, 2025
Merged

ilevkivskyi merged 1 commit into
python:masterfrom
ilevkivskyi:more-skip-body

Conversation

Copy link
Copy Markdown
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.

github-actions Bot commented Aug 4, 2025

Copy link
Copy Markdown
Contributor

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

sterliakov left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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

Comment thread mypy/nodes.py
# 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
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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).

hauntsaninja left a comment •
edited
Loading

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Nice!

re self check: naively my guess would just be pytest

Copy link
Copy Markdown
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 merged commit 83fed5a into python:master Aug 5, 2025
ilevkivskyi deleted the more-skip-body branch August 5, 2025 13:26
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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


Back | FazBrowse Home | New Git URL