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.
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
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 |
|
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.
619cf28
to
83ee297
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
torchtypinglibrary - 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,
mypyidentifies the problems with ourDictannotations, but is happy withAnnotatedand can be directed to ignoreTensorType:This PR therefore fixes #10266; and closes #11279. Thanks to @sobolevn for pointing me in the right direction!