Thanks to visit codestin.com
Credit goes to github.com

Skip to content

ast: Precise type for ast.Constant.value #14159

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 1 commit into from
May 27, 2025

Conversation

JelleZijlstra
Copy link
Member

These are the only types that Python's parser will produce.

Fixes #14156. See also python/cpython#134741.

Copy link
Contributor

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

cibuildwheel (https://github.com/pypa/cibuildwheel)
+ cibuildwheel/projectfiles.py:73: error: Incompatible types in assignment (expression has type "str | bytes | int | float | complex | EllipsisType | None", variable has type "str | None")  [assignment]

mypy (https://github.com/python/mypy)
+ mypy/fastparse.py:2063: error: Statement is unreachable  [unreachable]
+ mypy/fastparse.py:2063: note: See https://mypy.rtfd.io/en/stable/_refs.html#code-unreachable for more info

werkzeug (https://github.com/pallets/werkzeug)
+ src/werkzeug/routing/rules.py:779: error: No overload variant of "__add__" of "str" matches argument type "bytes"  [operator]
+ src/werkzeug/routing/rules.py:779: note: Possible overload variants:
+ src/werkzeug/routing/rules.py:779: note:     def __add__(self, str, /) -> str
+ src/werkzeug/routing/rules.py:779: error: No overload variant of "__add__" of "str" matches argument type "int"  [operator]
+ src/werkzeug/routing/rules.py:779: error: No overload variant of "__add__" of "str" matches argument type "float"  [operator]
+ src/werkzeug/routing/rules.py:779: error: No overload variant of "__add__" of "str" matches argument type "complex"  [operator]
+ src/werkzeug/routing/rules.py:779: error: No overload variant of "__add__" of "str" matches argument type "None"  [operator]
+ src/werkzeug/routing/rules.py:779: error: No overload variant of "__add__" of "str" matches argument type "ellipsis"  [operator]
+ src/werkzeug/routing/rules.py:779: error: Unsupported operand types for + ("bytes" and "str")  [operator]
+ src/werkzeug/routing/rules.py:779: error: Unsupported operand types for + ("bytes" and "int")  [operator]
+ src/werkzeug/routing/rules.py:779: error: Unsupported operand types for + ("bytes" and "float")  [operator]
+ src/werkzeug/routing/rules.py:779: error: Unsupported operand types for + ("bytes" and "complex")  [operator]
+ src/werkzeug/routing/rules.py:779: error: Unsupported operand types for + ("bytes" and "None")  [operator]
+ src/werkzeug/routing/rules.py:779: error: Unsupported operand types for + ("bytes" and "ellipsis")  [operator]
+ src/werkzeug/routing/rules.py:779: error: Unsupported operand types for + ("int" and "str")  [operator]
+ src/werkzeug/routing/rules.py:779: error: Unsupported operand types for + ("int" and "bytes")  [operator]
+ src/werkzeug/routing/rules.py:779: error: Unsupported operand types for + ("int" and "None")  [operator]
+ src/werkzeug/routing/rules.py:779: error: Unsupported operand types for + ("int" and "ellipsis")  [operator]
+ src/werkzeug/routing/rules.py:779: error: Unsupported operand types for + ("float" and "str")  [operator]
+ src/werkzeug/routing/rules.py:779: error: Unsupported operand types for + ("float" and "bytes")  [operator]
+ src/werkzeug/routing/rules.py:779: error: Unsupported operand types for + ("float" and "None")  [operator]
+ src/werkzeug/routing/rules.py:779: error: Unsupported operand types for + ("float" and "ellipsis")  [operator]
+ src/werkzeug/routing/rules.py:779: error: Unsupported operand types for + ("complex" and "str")  [operator]
+ src/werkzeug/routing/rules.py:779: error: Unsupported operand types for + ("complex" and "bytes")  [operator]
+ src/werkzeug/routing/rules.py:779: error: Unsupported operand types for + ("complex" and "None")  [operator]
+ src/werkzeug/routing/rules.py:779: error: Unsupported operand types for + ("complex" and "ellipsis")  [operator]
+ src/werkzeug/routing/rules.py:779: error: Unsupported left operand type for + ("None")  [operator]
+ src/werkzeug/routing/rules.py:779: error: Unsupported operand types for + ("None" and "int")  [operator]
+ src/werkzeug/routing/rules.py:779: error: Unsupported operand types for + ("None" and "float")  [operator]
+ src/werkzeug/routing/rules.py:779: error: Unsupported operand types for + ("None" and "complex")  [operator]
+ src/werkzeug/routing/rules.py:779: error: Unsupported left operand type for + ("ellipsis")  [operator]
+ src/werkzeug/routing/rules.py:779: error: Unsupported operand types for + ("ellipsis" and "int")  [operator]
+ src/werkzeug/routing/rules.py:779: error: Unsupported operand types for + ("ellipsis" and "float")  [operator]
+ src/werkzeug/routing/rules.py:779: error: Unsupported operand types for + ("ellipsis" and "complex")  [operator]
+ src/werkzeug/routing/rules.py:779: note: Both left and right operands are unions

flake8-pyi (https://github.com/PyCQA/flake8-pyi)
+ flake8_pyi/visitor.py:453: error: Item "bytes" of "str | bytes | int | float | complex | EllipsisType | None" has no attribute "isidentifier"  [union-attr]
+ flake8_pyi/visitor.py:453: error: Item "int" of "str | bytes | int | float | complex | EllipsisType | None" has no attribute "isidentifier"  [union-attr]
+ flake8_pyi/visitor.py:453: error: Item "float" of "str | bytes | int | float | complex | EllipsisType | None" has no attribute "isidentifier"  [union-attr]
+ flake8_pyi/visitor.py:453: error: Item "complex" of "str | bytes | int | float | complex | EllipsisType | None" has no attribute "isidentifier"  [union-attr]
+ flake8_pyi/visitor.py:453: error: Item "None" of "str | bytes | int | float | complex | EllipsisType | None" has no attribute "isidentifier"  [union-attr]
+ flake8_pyi/visitor.py:453: error: Item "EllipsisType" of "str | bytes | int | float | complex | EllipsisType | None" has no attribute "isidentifier"  [union-attr]
+ flake8_pyi/visitor.py:453: error: Argument 1 to "iskeyword" has incompatible type "str | bytes | int | float | complex | EllipsisType | None"; expected "str"  [arg-type]

prefect (https://github.com/PrefectHQ/prefect)
+ src/prefect/utilities/callables.py:655: error: Incompatible return value type (got "Union[str, bytes, int, float, complex, ellipsis, None]", expected "Optional[str]")  [return-value]
+ src/prefect/utilities/_ast.py:104: error: Dict entry 0 has incompatible type "str": "Union[str, bytes, int, float, complex, ellipsis, None]"; expected "str": "str"  [dict-item]

@AlexWaygood
Copy link
Member

AlexWaygood commented May 26, 2025

It would be nice if ast.Constant was generic so that this function could return TypeGuard[list[ast.Constant[str]]] instead of TypeGuard[list[ast.Constant]]: https://github.com/PyCQA/flake8-pyi/blob/6051cb1df58b23531e8aa4707d000e4338bbbdad/flake8_pyi/visitor.py#L419-L422

That would fix all of the flake8-pyi errors. I've had similar thoughts about e.g. ast.BinOp in the past, too -- it would be nice to be able to specify that a function can only take an ast.BinOp[ast.BitOr] if you're messing around with PEP-604 unions.

@JelleZijlstra
Copy link
Member Author

Some new errors from code that doesn't handle all kinds of constants.

I think the cibuildwheel one is a real latent bug. The mypy one is fallback for unrecognized constants; it seems positive that the type system now recognizes it's unreachable. The flake8-pyi one is a false positive because there's a check that they're all strs that is not visible to the type system. The werkzeug one is manually building up a list of str ast.Constants, so it's safe but the type system doesn't recognize it.

@JelleZijlstra
Copy link
Member Author

Making it generic would be troublesome because it would have to be invariant.

JelleZijlstra added a commit to PyCQA/flake8-pyi that referenced this pull request May 26, 2025
Copy link
Collaborator

@srittau srittau left a comment

Choose a reason for hiding this comment

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

The primer hits – except from the mypy one – are all either true positives or could at least benefit from some more defensive coding.

@srittau srittau merged commit c7e29ec into python:main May 27, 2025
64 checks passed
@cdce8p cdce8p mentioned this pull request Jun 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Accurate annotation for ast.Constant.value
3 participants