-
-
Notifications
You must be signed in to change notification settings - Fork 3k
[PEP 747] Recognize TypeForm[T] type and values (#9773) #19596
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
Open
davidfstr
wants to merge
21
commits into
python:master
Choose a base branch
from
davidfstr:f/typeform4
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,694
−74
Conversation
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
User must opt-in to use TypeForm with --enable-incomplete-feature=TypeForm In particular: * Recognize TypeForm[T] as a kind of type that can be used in a type expression * Recognize a type expression literal as a TypeForm value in: - assignments - function calls - return statements * Define the following relationships between TypeForm values: - is_subtype - join_types - meet_types * Recognize the TypeForm(...) expression * Alter isinstance(typx, type) to narrow TypeForm[T] to Type[T]
In particular: - Adjust error messages to use lowercased type names, which is now the default - Adjust error message to align with upstream stub changes - Fix multiple definition of TypeForm in typing_extensions.pyi, because definition was added upstream - Fix TypeType equality definition to recognize type forms - Fixes test: $ pytest -q -k testTypeFormToTypeAssignability
…nied note to standalone error
…te to a new context manager
...at the most-targeted location Specific warning: * SyntaxWarning: invalid escape sequence '\('
…nd the --dump-build-stats option
...in SemanticAnalyzer.try_parse_as_type_expression()
…d by TA.try_parse_as_type_expression()
…n checking open source code
for more information, see https://pre-commit.ci
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
for more information, see https://pre-commit.ci
Diff from mypy_primer, showing the effect of this PR on open source code: discord.py (https://github.com/Rapptz/discord.py)
- ...venv/lib/python3.13/site-packages/mypy/typeshed/stdlib/typing.pyi:1016: note: "update" of "TypedDict" defined here
+ ...venv/lib/python3.13/site-packages/mypy/typeshed/stdlib/typing.pyi:1017: note: "update" of "TypedDict" defined here
spark (https://github.com/apache/spark)
- python/pyspark/pandas/supported_api_gen.py:400: SyntaxWarning: invalid escape sequence '\_'
- return func_str[:-1] + "\_" # noqa: W605
|
I fixed all the "only in CI" check issues so this PR is now actually ready for review. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
(This PR replaces an earlier draft of the same feature: #18690 )
Feedback from @JukkaL integrated since the last PR, by commit title:
Feedback NOT integrated, with rationale:
Type[...]
, which I can fix in a separate PR.TypeForm(...)
in value contexts as a regular function likeCallable[[TypeForm[T]], TypeForm[T]]
rather than as a special expression node (TypeFormExpr).TypeForm(...)
. See case 4 of testTypeFormExpression in check-typeform.testThere is one NOMERGE commit temporarily in this PR so that mypy_primer gives more insightful CI output:
There is one commit unrelated to the core function of this PR that could be split to a separate PR:
Closes #9773
(Most of the following description is copied from the original PR, except for the text in bold)
Implements the TypeForm PEP 747, as an opt-in feature enabled by the CLI flag
--enable-incomplete-feature=TypeForm
.Implementation approach:
The
TypeForm[T]
is represented as a type using the existingTypeType
class, with anis_type_form=True
constructor parameter.Type[C]
continues to be represented usingTypeType
, but withis_type_form=False
(the default).Recognizing a type expression literal such as
int | str
requires parsing anExpression
as a type expression. Only the SemanticAnalyzer pass has the ability to parse arbitrary type expressions (including stringified annotations), usingSemanticAnalyzer.expr_to_analyzed_type()
. (I've extended theTypeChecker
pass to parse all kinds of type expressions except stringified annotations, using the newTypeCheckerAsSemanticAnalyzer
adapter.)Therefore during the SemanticAnalyzer pass, at certain syntactic locations (i.e. assignment r-values, callable arguments, returned expressions), the analyzer tries to parse the
Expression
it is looking at usingtry_parse_as_type_expression()
- a new function - and stores the result (aType
) in{IndexExpr, OpExpr, StrExpr}.as_type
- a new attribute.During the later TypeChecker pass, when looking at an
Expression
to determine its type, if the expression is in a type context that expects some kind ofTypeForm[...]
and the expression was successfully parsed as a type expression by the earlier SemanticAnalyzer pass (or can be parsed as a type expression immediately during the type checker pass), the expression will be given the typeTypeForm[expr.as_type]
rather than using the regular type inference rules for a value expression.Key relationships between
TypeForm[T]
,Type[C]
, andobject
types are defined in the visitors poweringis_subtype
,join_types
, andmeet_types
.The
TypeForm(T)
expression is recognized as aTypeFormExpr
and has the return typeTypeForm[T]
.The new test suite in
check-typeform.test
is a good reference to the expected behaviors for operations that interact withTypeForm
in some way.Controversial parts of this PR, in @davidfstr 's opinion:
Type form literals containing stringified annotations are only recognized in certain syntactic locations (and not ALL possible locations). Namely they are recognized as (1) assignment r-values, (2) callable expression arguments, and (3) as returned expressions, but nowhere else. For example they aren't recognized in expressions like
dict_with_typx_keys[int | str]
. Attempting to use stringified annotations in other locations will emit a MAYBE_UNRECOGNIZED_STR_TYPEFORM error.The existing
TypeType
class is now used to represent BOTH theType[T]
andTypeForm[T]
types, rather than introducing a distinct subclass ofType
to represent theTypeForm[T]
type. This was done to simplify logic that manipulates bothType[T]
andTypeForm[T]
values, since they are both manipulated in very similar ways.The "normalized" form of
TypeForm[X | Y]
- as returned byTypeType.make_normalized()
- is justTypeForm[X | Y]
rather thanTypeForm[X] | TypeForm[Y]
, differing from the normalization behavior ofType[X | Y]
.