Skip to content
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

Allow new-style self-types in classmethods #17381

Merged
merged 6 commits into from
Jun 17, 2024

Conversation

ilevkivskyi
Copy link
Member

@ilevkivskyi ilevkivskyi commented Jun 13, 2024

Fixes #16547
Fixes #16410
Fixes #5570

From the upvotes on the issue it looks like an important use case. From what I see this is an omission in the original implementation, I don't see any additional unsafety (except for the same that exists for instance methods/variables). I also incorporate a small refactoring and remove couple unused get_proper_type() calls.

The fix uncovered an unrelated issue with unions in descriptors, so I fix that one as well.

This comment has been minimized.

This comment has been minimized.

This comment has been minimized.

@ilevkivskyi
Copy link
Member Author

Hm, interestingly the last commit also fixed the primer somehow. Although this may seem good, it is actually suspicious, it may be that we handle access to self-type on unions incorrectly. Let me check this.

@ilevkivskyi
Copy link
Member Author

Oh, yup, we incorrectly use mx.original_type instead of mx.self_type in two places: in new-style self-types, and in descriptor access logic. Why it is always a can of worms...

Copy link
Contributor

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

pandas (https://github.com/pandas-dev/pandas)
- pandas/core/apply.py:254: error: No overload variant of "__get__" of "AxisProperty" matches argument types "GroupBy[Any]", "type[GroupBy[Any]]"  [call-overload]
- pandas/core/apply.py:254: note: Error code "call-overload" not covered by "type: ignore" comment
- pandas/core/apply.py:254: note: Possible overload variants:
- pandas/core/apply.py:254: note:     def __get__(self, obj: DataFrame | Series, type: Any) -> Index
- pandas/core/apply.py:254: note:     def __get__(self, obj: None, type: Any) -> AxisProperty
- pandas/core/apply.py:254: error: No overload variant of "__get__" of "AxisProperty" matches argument types "SeriesGroupBy", "type[SeriesGroupBy]"  [call-overload]
- pandas/core/apply.py:254: error: No overload variant of "__get__" of "AxisProperty" matches argument types "DataFrameGroupBy", "type[DataFrameGroupBy]"  [call-overload]
- pandas/core/apply.py:254: error: No overload variant of "__get__" of "AxisProperty" matches argument types "BaseWindow", "type[BaseWindow]"  [call-overload]
- pandas/core/apply.py:254: error: No overload variant of "__get__" of "AxisProperty" matches argument types "Resampler", "type[Resampler]"  [call-overload]

@ilevkivskyi
Copy link
Member Author

Why it is always a can of worms...

In fairness it looks like this time I contributed to both issues, LOL. In any case I am now happy with mypy_primer, so I am going to merge this soon, unless there are objections.

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 to see some many bugs fixed!

@ilevkivskyi ilevkivskyi merged commit ba5c279 into python:master Jun 17, 2024
18 checks passed
@ilevkivskyi ilevkivskyi deleted the fix-self-cls branch June 17, 2024 23:45
asottile added a commit to asottile/django-stubs that referenced this pull request Jul 25, 2024
there's a lot going on in the test but this specifically breaks lookup through
`TypeVar` in mypy 1.11 (mypy 1.10 also failed in this way as well -- but was
fixed in python/mypy#17381)

test originally failing with:

```
/tmp/django-stubs/tests/typecheck/models/test_inheritance.yml:114:
E   pytest_mypy_plugins.utils.TypecheckAssertionError: Invalid output:
E   Actual:
E     myapp/models:23: error: Incompatible return value type (got "Bound", expected "T")  [return-value] (diff)
E   Expected:
E     (empty)
```
asottile added a commit to asottile/django-stubs that referenced this pull request Jul 25, 2024
there's a lot going on in the test but this specifically breaks lookup through
`TypeVar` in mypy 1.11 (mypy 1.10 also failed in this way as well -- but was
fixed in python/mypy#17381)

test originally failing with:

```
/tmp/django-stubs/tests/typecheck/models/test_inheritance.yml:114:
E   pytest_mypy_plugins.utils.TypecheckAssertionError: Invalid output:
E   Actual:
E     myapp/models:23: error: Incompatible return value type (got "Bound", expected "T")  [return-value] (diff)
E   Expected:
E     (empty)
```
sobolevn pushed a commit to typeddjango/django-stubs that referenced this pull request Jul 25, 2024
there's a lot going on in the test but this specifically breaks lookup through
`TypeVar` in mypy 1.11 (mypy 1.10 also failed in this way as well -- but was
fixed in python/mypy#17381)

test originally failing with:

```
/tmp/django-stubs/tests/typecheck/models/test_inheritance.yml:114:
E   pytest_mypy_plugins.utils.TypecheckAssertionError: Invalid output:
E   Actual:
E     myapp/models:23: error: Incompatible return value type (got "Bound", expected "T")  [return-value] (diff)
E   Expected:
E     (empty)
```
asottile-sentry added a commit to getsentry/sentry that referenced this pull request Jul 31, 2024
<!-- Describe your PR here. -->

this had three sets of breakage addressed by other PRs:

- our foreign key subclass was not functioning, django-stubs added a
default TypeVar here which started getting filled in with an unbound
TypeVar resulting in thousands of errors: fixed by
#75228
- we were able to remove our fork's [descriptor
patch](getsentry/sentry-forked-django-stubs#4)
(which removed the non-model overload of `__get__` for fields) as mypy
[fixed this issue](python/mypy#17381). in doing
so it pointed out an unsafe descriptor access through a mixin and so
that had to go: #75360
- django-stubs improved some field validation through QuerySets which
was only checked through managers before: fixed by
#75359
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants