-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Support for PEP 695 #15238
Comments
Just to note — even if mypy implemented support for PEP 695 tomorrow, it will probably be quite a while before typeshed is able to use PEP 695 syntax, and I imagine the same will be true for many stub authors. Mypy uses the Python |
Mypy does not yet support PEP 695 Fixes python#16011, linking python#15238
Mypy does not yet support PEP 695 Fixes python#16011, linking python#15238
Python3.12 has been out for a while now, is there a timeline for when the new generics will be supported? |
When you (or anyone else reading this) submit a PR implementing it. Mypy is a volunteer-run project and has no set timelines. I did a little preliminary work on this myself, but not sure I'll be able to complete it any time soon. |
Haven't contributed to mypy yet, so, just asking: is it required that all the features of PEP 695 be implemented in the scope of one issue/PR? Is there something speaking against e.g. implementing support for the |
No, implementing parts of it one by one is fine. We could consider using the Here's my partial attempt to implement support for One thing I wasn't sure about was whether to create a new |
That would be very appreciated 😄! As I can't seem to suppress this def chunks[T](seq: Sequence[T], n: int) -> Generator[Sequence[T], None, None]: # type: ignore
for i in range(0, len(seq), n):
yield seq[i : i + n]
|
How do I disable this check in a configuration file? It seems like it would make more sense to warn on this or ignore it rather than error if it's simply not supported by MyPy, but for now, how do I tell MyPy to globally not worry about it? Thank you! Is disable_error_code = valid-type enough? Is |
I filed issue #16607 since it doesn't not appear possible to instruct Mypy to ignore this error via any of the existing ignore error methods. This seems somewhat critical to me in that it does not allow Mypy to be used in 3.12 projects requiring strict Mypy passes. |
I started looking into this. So far the only tricky bit I've encountered is inferring variance. To infer variance, we need to know the types of all attributes, so it needs to happen after type checking -- but we need to know the variances of type variables to perform type checking. The way we normally solve issues like this is to use deferral. Any code that needs to know the variance of a type variable defined in the same SCC would be deferred. After the first pass of type checking, and before type checking deferred targets again, we'd attempt to infer variances. The inference itself seems easy and very similar to what we already do when validating that variance is correct in protocols. We might need to do a few passes of variance inference in complex cases, and if inference fails, it may be best to assume the type variable is invariant (or covariant?) instead of generating an error. @ilevkivskyi pointed out that we may need to defer module top levels, but this isn't supported right now. So it looks like we'll need to solve that first. Deferring top levels is very tricky when not using local partial types ( Another idea I got from @ilevkivskyi is to build a tool to help migrate code to |
At least two are required, according do PEP 695. Work on #15238.
At least two are required, according do PEP 695. Work on #15238.
Note for tracking: we now have experimental support for the new syntax, thanks to @JukkaL's work. I think we can close this issue once the support becomes non-experimental. I added
topic-pep-695
|
Am I missing something? |
Even the experimental support only exists on the |
For what it's worth I used this feature in a work project a week ago, running a version of Mypy straight from the master branch, and it worked flawlessly. |
Detect invalid number of constrained types. At least two are required, according do PEP 695. Add tests for other simple errors. Work on #15238.
[UP040[(https://docs.astral.sh/ruff/rules/non-pep695-type-alias/) warns on the use of `TypeAlias` instead of the new `type` keyword. But: while Python 3.12 supports the `type` keyword, mypy [doesn't yet](python/mypy#15238).
[UP040[(https://docs.astral.sh/ruff/rules/non-pep695-type-alias/) warns on the use of `TypeAlias` instead of the new `type` keyword. But: while Python 3.12 supports the `type` keyword, mypy [doesn't yet](python/mypy#15238).
TODO: Switch back to PEP 695 type alias when supported by mypy (python/mypy#15238)
TODO: Switch back to PEP 695 type alias when supported by mypy (python/mypy#15238)
TODO: Switch back to PEP 695 type alias when supported by mypy (python/mypy#15238)
…xpressions within the scope of a type parameter and a type alias (#17560) This fixes part of #15238: > Add detection and error reporting for use of assignment expressions, yield, yield from, and await expressions within the type parameter scope; see [this section of PEP](https://peps.python.org/pep-0695/#type-parameter-scopes) for details --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: sobolevn <mail@sobolevn.me>
Works beautifully, well done team ! |
TODO: Switch back to PEP 695 type alias when supported by mypy (python/mypy#15238)
TODO: Switch back to PEP 695 type alias when supported by mypy (python/mypy#15238)
Now that PEP 695 has been enabled by default on master, I'm closing this issue. There are still some known gaps in the functionality and a few open issues. We can track any remaining work in separate issues using the topic-pep-695 label. |
PEP 695 was accepted, and it looks like it will make it into Python 3.12. This PEP adds support for a new type parameter syntax for generic classes and methods. It also adds a new syntax for type aliases (both generic and not).
PEP 695 functionality is implemented in pyright today. It would be great to see it also implemented in mypy and the other major type checkers so we could start to use the new features in type stubs.
Here is a rough task list associated with this work:
infer_variance
keyword parameter inTypeVar
; see this section of PEP for detailstype
statement; see this section of PEP for detailstype
statement (including generic and recursive type definitions); see this section of PEP for detailsTypeAliasType
, supported for backward compatibility; see this section of PEP for detailsThe text was updated successfully, but these errors were encountered: