-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Mark more things as literals #20604
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
base: master
Are you sure you want to change the base?
Mark more things as literals #20604
Changes from all commits
57ae6c6
2a869b0
b98c82a
f3b23b1
d836ead
45185ed
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -35,6 +35,7 @@ | |
| OpExpr, | ||
| ParamSpecExpr, | ||
| PromoteExpr, | ||
| RefExpr, | ||
| RevealExpr, | ||
| SetComprehension, | ||
| SetExpr, | ||
|
|
@@ -44,6 +45,7 @@ | |
| SuperExpr, | ||
| TempNode, | ||
| TupleExpr, | ||
| TypeAlias, | ||
| TypeAliasExpr, | ||
| TypeApplication, | ||
| TypedDictExpr, | ||
|
|
@@ -55,6 +57,7 @@ | |
| YieldExpr, | ||
| YieldFromExpr, | ||
| ) | ||
| from mypy.types import is_named_instance | ||
| from mypy.visitor import ExpressionVisitor | ||
|
|
||
| # [Note Literals and literal_hash] | ||
|
|
@@ -97,6 +100,9 @@ | |
|
|
||
|
|
||
| Key: _TypeAlias = tuple[Any, ...] | ||
| LITERAL_NAMES: Final = frozenset( | ||
| ("builtins.True", "builtins.False", "builtins.None", "builtins.NotImplemented") | ||
| ) | ||
|
|
||
|
|
||
| def literal_hash(e: Expression) -> Key | None: | ||
|
|
@@ -135,10 +141,23 @@ def literal(e: Expression) -> int: | |
| else: | ||
| return LITERAL_NO | ||
|
|
||
| elif isinstance(e, NameExpr): | ||
| if isinstance(e.node, Var) and e.node.is_final and e.node.final_value is not None: | ||
| elif isinstance(e, RefExpr): | ||
| if ( | ||
| isinstance(e, NameExpr) | ||
| and isinstance(e.node, Var) | ||
| and e.node.is_final | ||
| and e.node.final_value is not None | ||
| ): | ||
| return LITERAL_YES | ||
| return LITERAL_TYPE | ||
|
|
||
| if e.fullname in LITERAL_NAMES: | ||
| return LITERAL_YES | ||
| elif isinstance(e.node, TypeAlias) and not e.node.python_3_12_type_alias: | ||
| if is_named_instance(e.node.target, LITERAL_NAMES): | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can a type alias refer to anything but None out of the options here? If this is case, maybe avoid
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I basically just copied over previous code to prevent changes in behavior, but I don't think this can actually happen. (this is here because |
||
| return LITERAL_YES | ||
|
|
||
| if isinstance(e, NameExpr): | ||
| return LITERAL_TYPE | ||
|
|
||
| if isinstance(e, (IntExpr, FloatExpr, ComplexExpr, StrExpr, BytesExpr)): | ||
| return LITERAL_YES | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.