-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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 slice syntax in e.g. Annotated[int, 1:3]
and TensorType["batch":..., float]
#11345
Conversation
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
except that there's some weird interaction on Python < 3.9, which I don't understand well enough to track down quickly. Any suggestions?
Python3.9 has different ast.Subscript
structure. This helper might be useful to understand how it changed: https://github.com/wemake-services/wemake-python-styleguide/blob/master/wemake_python_styleguide/compat/functions.py#L32-L45
020223b
to
af0e198
Compare
This comment has been minimized.
This comment has been minimized.
Try to run your tests with
Column numbers might be different here: https://github.com/python/mypy/pull/11345/files#diff-a7d2c40f4be6322cee498432ca9dbbc3a356c3cbcb61029231452f6638bb11b5R1565-R1572 |
1cd724d
to
e095098
Compare
Alright, I've fixed the pre-Python-3.9 issues: with message ordering (by propagating column numbers), and with However... this causes a problem, where on Python 3.9 |
@Zac-HD will conditional type alias work in this case? 🤔 if sys.version_info >= (3, 9):
AstIndex = Any
else:
AstIndex = ast.Index |
Or just imitate |
e095098
to
efcd2a9
Compare
Woohoo, it's all working! Thanks for your help @sobolevn 🥰 Ping @JelleZijlstra for review? |
This is useful for Annotated, and crucial for downstream libraries like torchtyping.
efcd2a9
to
22e8e3a
Compare
619cf28
to
83ee297
Compare
83ee297
to
a9f151c
Compare
This is useful for Annotated, and crucial for downstream libraries like torchtyping. This PR cherrypicks #11345 onto the 0.920 release branch
…ch":..., float]` (python#11345) This is useful for Annotated, and crucial for downstream libraries like torchtyping.
Consider the following trivial Python file, using the
torchtyping
library - if only this typechecked, we could write some fancy ML code!On
master
, we get syntax-level errors due to the use of:
slice syntax, which prevents any semantic checking - and can't even be disabled by a config file!Following this patch,
mypy
identifies the problems with ourDict
annotations, but is happy withAnnotated
and can be directed to ignoreTensorType
:This PR therefore fixes #10266; and closes #11279. Thanks to @sobolevn for pointing me in the right direction!