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

Handle assignment of bound methods in class bodies by ilevkivskyi · Pull Request #19233 · python/mypy · GitHub

Repository navigation

Handle assignment of bound methods in class bodies - #19233

Merged
ilevkivskyi merged 2 commits into
python:masterfrom
ilevkivskyi:handle-bound-assign
Jun 5, 2025
Merged

ilevkivskyi merged 2 commits into
python:masterfrom
ilevkivskyi:handle-bound-assign

Conversation

ilevkivskyi commented Jun 4, 2025 •
edited
Loading

Copy link
Copy Markdown
Member

Fixes #18438
Fixes #19146

Surprisingly, a very small change is sufficient to replicate Python runtime behavior for all the important cases (see checkmember.py). I also replace the bound_args argument of CallableType, that was mostly unused, with a flag (as suggested by @JukkaL) and make sure it is properly set/preserved everywhere.

This comment has been minimized.

JukkaL 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

Nice! The mypy primer error message change in mongo-python-driver looks odd though -- do you know what is going on there?

==
main:3: error: Property "f" defined in "A" is read-only

[case testMethodMakeBoundFineGrained]

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

Add also normal incremental mode (serialization) tests case?

x1 = A.f
x2 = A.g
x3 = A().f
x4 = A().g

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

Would it make sense to test @staticmethod as well?

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

And maybe normal attribute with an explicit annotated Callable type?

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

Static methods are actually unrelated to this story, they are already handled using special-casing here https://github.com/python/mypy/blob/master/mypy/checker.py#L4420-L4422. But I still added the tests, because existing tests only cover static method alias within same class. (Also attributes with explicit callable annotations are considered instance attributes, but anyway added for completeness.)

ilevkivskyi commented Jun 5, 2025 •
edited
Loading

Copy link
Copy Markdown
Member Author

@JukkaL

The mypy primer error message change in mongo-python-driver looks odd though -- do you know what is going on there?

Yes, the same thing that is going on in check-classes.test. Using object_type in messages.py instead of bound_args[0] is not a 1:1 change, but IMO new messages are equally good.

github-actions Bot commented Jun 5, 2025

Copy link
Copy Markdown
Contributor

Diff from mypy_primer, showing the effect of this PR on open source code:

prefect (https://github.com/PrefectHQ/prefect)
- src/prefect/_internal/concurrency/threads.py:37: error: Attribute function "_counter" with type "Callable[[], int]" does not accept self argument  [misc]

jinja (https://github.com/pallets/jinja)
+ src/jinja2/runtime.py:370: error: Unused "type: ignore" comment  [unused-ignore]
+ src/jinja2/runtime.py:384: error: Unused "type: ignore" comment  [unused-ignore]
+ src/jinja2/environment.py:1290: error: Unused "type: ignore" comment  [unused-ignore]
+ src/jinja2/environment.py:1311: error: Unused "type: ignore" comment  [unused-ignore]

mongo-python-driver (https://github.com/mongodb/mongo-python-driver)
- pymongo/synchronous/cursor.py:1004: error: Invalid index type "int | Any | Pattern[Any]" for "list[Any]"; expected type "SupportsIndex"  [index]
+ pymongo/synchronous/cursor.py:1004: error: Invalid index type "int | Any | Pattern[Any]" for "list"; expected type "SupportsIndex"  [index]
- pymongo/asynchronous/cursor.py:1006: error: Invalid index type "int | Any | Pattern[Any]" for "list[Any]"; expected type "SupportsIndex"  [index]
+ pymongo/asynchronous/cursor.py:1006: error: Invalid index type "int | Any | Pattern[Any]" for "list"; expected type "SupportsIndex"  [index]

ilevkivskyi merged commit b6f2ea3 into python:master Jun 5, 2025
ilevkivskyi deleted the handle-bound-assign branch June 5, 2025 14:09

delfick commented Jun 5, 2025

Copy link
Copy Markdown

Thanks!

JukkaL pushed a commit that referenced this pull request Jun 11, 2025
Fixes #18438
Fixes #19146

Surprisingly, a very small change is sufficient to replicate Python
runtime behavior for all the important cases (see `checkmember.py`). I
also replace the `bound_args` argument of `CallableType`, that was
mostly unused, with a flag (as suggested by @JukkaL) and make sure it is
properly set/preserved everywhere.
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

3 participants


Back | FazBrowse Home | New Git URL