Python Type Hint Syntax Error. Need help for "Optional[Tuple[int, set[int]]]" #199748
-
🏷️ Discussion TypeBug BodyToday while upgrading my Python environment... and I encountered a type hint issue that's causing errors in my IDE! Here's the problematic code: in_switch: Optional[Tuple[int, set[int]]] = Noneand on here: items: Optional[List["Expr"]] = NoneMy IDE (PyCharm) is showing an error message: I've tried searching for solutions but I'm stuck... The IDE's inspection info shows examples like: from typing import TypeVar
T0 = TypeVar('T1') # Error: Argument of 'TypeVar' must be 'T0'
def b(p: int) -> int: # Error: Type specified both in comment and annotation
# type: (int) -> int
passBut none of these seem to match my case directly really... Any help would be greatly appreciated! :) |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
|
💬 Your Product Feedback Has Been Submitted 🎉 Thank you for taking the time to share your insights with us! Your feedback is invaluable as we build a better GitHub experience for all our users. Here's what you can expect moving forward ⏩
Where to look to see what's shipping 👀
What you can do in the meantime 💻
As a member of the GitHub community, your participation is essential. While we can't promise that every suggestion will be implemented, we want to emphasize that your feedback is instrumental in guiding our decisions and priorities. Thank you once again for your contribution to making GitHub even better! We're grateful for your ongoing support and collaboration in shaping the future of our platform. ⭐ |
Beta Was this translation helpful? Give feedback.
-
|
The issue is that you're mixing type-hint syntax from different Python versions. This part: set[int]only works in Python 3.9+. So this is valid in Python 3.9+: from typing import Optional, Tuple
in_switch: Optional[Tuple[int, set[int]]] = NoneBut if your interpreter or IDE is configured for Python 3.8 or older, Use from typing import Optional, Tuple, Set
in_switch: Optional[Tuple[int, Set[int]]] = NoneSimilarly, this is already the compatible form: from typing import Optional, List
items: Optional[List["Expr"]] = NoneIf you want the most compatible Python 3 style, write both like this: from typing import List, Optional, Set, Tuple
in_switch: Optional[Tuple[int, Set[int]]] = None
items: Optional[List["Expr"]] = NoneIf you're using Python 3.10+, you can also write the newer style: in_switch: tuple[int, set[int]] | None = None
items: list["Expr"] | None = NoneSo the problem is most likely not Check your PyCharm project interpreter / language level. If it is set to Python 3.8 or older, either upgrade it or use |
Beta Was this translation helpful? Give feedback.
The issue is that you're mixing type-hint syntax from different Python versions.
This part:
only works in Python 3.9+.
So this is valid in Python 3.9+:
But if your interpreter or IDE is configured for Python 3.8 or older,
set[int]is invalid as a type hint.Use
Setfromtypingfor wider compatibility:Similarly, this is already the compatible form:
If you want the most compatible Python 3 style, write both like this: