Skip to content

Handle assignment of bound methods in class bodies #19233

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 2 commits into from
Jun 5, 2025

Conversation

ilevkivskyi
Copy link
Member

@ilevkivskyi ilevkivskyi commented Jun 4, 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 comment has been minimized.

Copy link
Collaborator

@JukkaL JukkaL left a comment

Choose a reason for hiding this comment

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

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

@@ -11217,3 +11217,27 @@ class A:
[out]
==
main:3: error: Property "f" defined in "A" is read-only

[case testMethodMakeBoundFineGrained]
Copy link
Collaborator

Choose a reason for hiding this comment

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

Add also normal incremental mode (serialization) tests case?

x1 = A.f
x2 = A.g
x3 = A().f
x4 = A().g
Copy link
Collaborator

Choose a reason for hiding this comment

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

Would it make sense to test @staticmethod as well?

Copy link
Collaborator

Choose a reason for hiding this comment

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

And maybe normal attribute with an explicit annotated Callable type?

Copy link
Member Author

Choose a reason for hiding this comment

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

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

ilevkivskyi commented Jun 5, 2025

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

Copy link
Contributor

github-actions bot commented Jun 5, 2025

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 ilevkivskyi merged commit b6f2ea3 into python:master Jun 5, 2025
19 checks passed
@ilevkivskyi ilevkivskyi deleted the handle-bound-assign branch June 5, 2025 14:09
@delfick
Copy link

delfick commented Jun 5, 2025

Thanks!

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