You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
file.py:4: error: syntax error in type comment
Found 1 error in 1 file (errors prevented further checking)
Your Environment
Mypy version used: 0.812
Mypy command-line flags: None
Mypy configuration options from mypy.ini (and other config files): None
Python version used: 3.9.2
Operating system and version: Windows 10
Further details
Any use of slices seems to give the error, e.g. Annotated[int, "a": 4].
I actually ran into this whilst implementing a type with a custom __class_getitem__, using slice notation as a convenient notation to pass pairs of values: MyType["key1": value1, "key2": value2]. Which I realise is a bit rogue -- I don't have any real hope of mypy integration for the type -- but it would at least be nice if it can be used without crashing mypy, with just a # type: ignore where it's imported.
I did some digging. The error is being raised from visit_Subscript in mypy.fastparse.TypeConverter. The code is different depending on the Python version but the error is the same.
Hi, Can I take this on? It's my very first issue! From what @domdfcoding has investigated, it seems that for py3.9, we can remove the constraint that the slice which is an ast.Tuple node not contain ast.Slice nodes. Looking at https://docs.python.org/3/library/ast.html#subscripting, I was wondering why that check was there in the first place since it says "It can be a Tuple and contain a Slice". For py3.8 and lower, the API documentation is not as clear as to what the slice can be but https://docs.python.org/3.8/library/ast.html indicates that slice is probably ast.Slice or ast.ExtSlice or ast.Index. So, for that I'm not sure, perhaps we can include that the slice can be ast.ExtSlice as well?
@JelleZijlstra I am trying to fix the bug, for the example given above it's an instance of ast.Tuple(ast.Name, ast.Slice) and the problem is that of stated by @domdfcoding, and we will need to modify the conditions , it is probably not a good idea to just remove the constraint, I will need a little bit of help with the new condition to replace it with.
## the following does not throw an error ### file.pyfromtypingimportAnnotateddeffunc(x: Annotated[int, {3:4}]): # pass a dict instead of slicespass# command linemypyfile.py
Cheers,
Andreas 😃
P.S.: If the decision is made to lift the currently in-place slice restriction, I can prepare a PR with a fix.
IMO these should still be errors, just not (misleading) syntax error in type comment errors:
"expects 2 type arguments, but 1 given" like Dict[int]), with a new "did you mean..." hint for slices.
"Invalid type comment or annotation" like Dict[int, {1: 2}]; which already "works" for custom types like torchtyping.TensorType.
and the tests could be updated accordingly, so that mypy continues to guide naive users towards the right way to express simple cases, without erroring out on more advanced or experimental uses.
I agree with @Zac-HD here. The error message should be improved, but Dict[int, x:y] and all of the example above should not be allowed (unless new PEPs or typing syntax changes are made). I would be super confused to see dict[x:y] as a type.
Proposed altertives are:
slice(1, 3) instead of 1:3
{x: y} instead of x: y
I will send a PR with the better error message today! 👍
I don't think that #11241 should be considered as closing this issue without @sobolevn's proposed follow-up:
fromtyping_extensionsimportAnnotateddeffunc(x: Annotated[int, 3:4]): # still a fatal error, contra (?) PEP-593x+"a string"# I expect an error here but don't get one
Activity
JelleZijlstra commentedon Mar 30, 2021
That's a fun one! It's probably pretty easy to fix if you track down where the error comes from (probably somewhere in
fastparse.py).domdfcoding commentedon Apr 1, 2021
I did some digging. The error is being raised from
visit_Subscriptinmypy.fastparse.TypeConverter. The code is different depending on the Python version but the error is the same.On Python 3.9 and later:
mypy/mypy/fastparse.py
Lines 1546 to 1551 in 2787d49
It is expecting the slice to be either:
ast.Sliceast.Tuplewhich doesn't contain anyast.Slicenodes.In this case, the slice is an
ast.Tuplenode containing:ast.Index(the nameint)ast.Slice(3:4)On Python 3.8 and earlier:
mypy/mypy/fastparse.py
Lines 1553 to 1557 in 2787d49
It is expecting the slice to be an
ast.Indexbut instead it is anast.ExtSlicecontaining:ast.Index(the nameint)ast.Slice(3:4)amrit110 commentedon Apr 23, 2021
Hi, Can I take this on? It's my very first issue! From what @domdfcoding has investigated, it seems that for py3.9, we can remove the constraint that the slice which is an
ast.Tuplenode not containast.Slicenodes. Looking at https://docs.python.org/3/library/ast.html#subscripting, I was wondering why that check was there in the first place since it says "It can be a Tuple and contain a Slice". For py3.8 and lower, the API documentation is not as clear as to what the slice can be but https://docs.python.org/3.8/library/ast.html indicates that slice is probablyast.Sliceorast.ExtSliceorast.Index. So, for that I'm not sure, perhaps we can include that the slice can beast.ExtSliceas well?PrasanthChettri commentedon Jun 6, 2021
@JelleZijlstra I am trying to fix the bug, for the example given above it's an instance of ast.Tuple(ast.Name, ast.Slice) and the problem is that of stated by @domdfcoding, and we will need to modify the conditions , it is probably not a good idea to just remove the constraint, I will need a little bit of help with the new condition to replace it with.
ap-- commentedon Jun 27, 2021
It seems like fixing this issue in
mypy.fastparse.TypeConverter.visit_Subscriptwould ultimately lead to allowing either both or at least one of:which is at least in one test explicitly marked to be a blocking error
mypy/test-data/unit/check-fastparse.test
Lines 320 to 325 in 3dcccf2
see: cd5ff81
and: 8ac365b
The docs for
typing.Annotatedhttps://docs.python.org/3/library/typing.html#typing.Annotated don't seem to suggest to use slicing syntax.@patrick-kidger It looks as if the original use case is basically trying to emulate keyword arguments in getitem similar to pep472/pep637 via the slicing notation. So maybe https://www.python.org/dev/peps/pep-0472/#pass-a-dictionary-as-an-additional-index could be an easy solution to get this working in torchtyping with only a tiny overhead in characters:
Cheers,
Andreas 😃
P.S.: If the decision is made to lift the currently in-place slice restriction, I can prepare a PR with a fix.
morp007 commentedon Aug 9, 2021
@patrick-kidger, there's no slice in your code. See python spec: Slicings.
Your code should look like this:
Please, close this issue.
Zac-HD commentedon Sep 29, 2021
IMO these should still be errors, just not (misleading)
syntax error in type commenterrors:Dict[int]), with a new "did you mean..." hint for slices.Dict[int, {1: 2}]; which already "works" for custom types liketorchtyping.TensorType.and the tests could be updated accordingly, so that
mypycontinues to guide naive users towards the right way to express simple cases, without erroring out on more advanced or experimental uses.sobolevn commentedon Oct 1, 2021
I agree with @Zac-HD here. The error message should be improved, but
Dict[int, x:y]and all of the example above should not be allowed (unless new PEPs or typing syntax changes are made). I would be super confused to seedict[x:y]as a type.Proposed altertives are:
slice(1, 3)instead of1:3{x: y}instead ofx: yI will send a PR with the better error message today! 👍
Improves error message for parsing `slice` in annotations (#11241)
Zac-HD commentedon Oct 4, 2021
I don't think that #11241 should be considered as closing this issue without @sobolevn's proposed follow-up:
As above, I think it's fine for slices to be an error in stdlib generics - but IMO ought to be allowed in
Annotatedand third-party generic types.