-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Adds TypeAlias
annotation, where possible
#11321
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
Conversation
Looks like it does not work out of the box. Some more work is required. |
Thanks for doing this, will take a look later. I suspect at least some of the confusion might just be from clobbering mypy.nodes.TypeAlias Lines 2877 to 2878 in c22beb4
|
mypy/checker.py
Outdated
@@ -8,7 +8,7 @@ | |||
Any, Dict, Set, List, cast, Tuple, TypeVar, Union, Optional, NamedTuple, Iterator, | |||
Iterable, Sequence, Mapping, Generic, AbstractSet, Callable | |||
) | |||
from typing_extensions import Final | |||
from typing_extensions import Final, TypeAlias |
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.
This file also imports mypy.nodes.TypeAlias (line 25)
mypy/checkexpr.py
Outdated
@@ -7,7 +7,7 @@ | |||
from typing import ( | |||
Any, cast, Dict, Set, List, Tuple, Callable, Union, Optional, Sequence, Iterator | |||
) | |||
from typing_extensions import ClassVar, Final, overload | |||
from typing_extensions import ClassVar, Final, overload, TypeAlias |
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.
This one too (line 34)
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.
Thanks! Now TypeAlias
from typing_extensions
is always called _TypeAlias
@hauntsaninja is right. I wonder why flake8 doesn't catch this. |
Looks like the CI is stuck. Reopening to retrigger it. |
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.
Hooray, looks like it's passing tests.
I'd recommend using _TypeAlias
in every file, not just ones that we have mypy.nodes.TypeAlias
in. Actually, thinking about it, I think the clearest thing to do is just use the fully qualified typing_extensions.TypeAlias
everywhere in mypy. There aren't too many of these, so the extra verbosity is probably worth it.
I think we'll also need to change the minimum version of typing_extensions
we require.
I've already done this 🎉
I cannot agree with this, sorry! This is way too verbose. Imagine this: import typing_extensions
TypeParameterChecker: typing_extensions.TypeAlias = Callable[[Type, Type, int], bool] Current solution: from typing_extensions import TypeAlias as _TypeAlias
TypeParameterChecker: _TypeAlias = Callable[[Type, Type, int], bool] Looks and reads much better! 😎 |
mypy/report.py
Outdated
@@ -45,7 +45,7 @@ | |||
] | |||
) | |||
|
|||
ReporterClasses = Dict[str, Tuple[Callable[['Reports', str], 'AbstractReporter'], bool]] | |||
ReporterClasses: TypeAlias = Dict[str, Tuple[Callable[['Reports', str], 'AbstractReporter'], bool]] |
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.
ReporterClasses: TypeAlias = Dict[str, Tuple[Callable[['Reports', str], 'AbstractReporter'], bool]] | |
ReporterClasses: _TypeAlias = Dict[str, Tuple[Callable[['Reports', str], 'AbstractReporter'], bool]] |
mypy/scope.py
Outdated
|
||
from mypy.backports import nullcontext | ||
from mypy.nodes import TypeInfo, FuncBase | ||
|
||
|
||
SavedScope = Tuple[str, Optional[TypeInfo], Optional[FuncBase]] | ||
SavedScope: TypeAlias = Tuple[str, Optional[TypeInfo], Optional[FuncBase]] |
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.
SavedScope: TypeAlias = Tuple[str, Optional[TypeInfo], Optional[FuncBase]] | |
SavedScope: _TypeAlias = Tuple[str, Optional[TypeInfo], Optional[FuncBase]] |
mypy/typestate.py
Outdated
|
||
from mypy.nodes import TypeInfo | ||
from mypy.types import Instance, TypeAliasType, get_proper_type, Type | ||
from mypy.server.trigger import make_trigger | ||
from mypy import state | ||
|
||
# Represents that the 'left' instance is a subtype of the 'right' instance | ||
SubtypeRelationship = Tuple[Instance, Instance] | ||
SubtypeRelationship: TypeAlias = Tuple[Instance, Instance] |
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.
SubtypeRelationship: TypeAlias = Tuple[Instance, Instance] | |
SubtypeRelationship: _TypeAlias = Tuple[Instance, Instance] |
mypy/typestate.py
Outdated
|
||
# A tuple encoding the specific conditions under which we performed the subtype check. | ||
# (e.g. did we want a proper subtype? A regular subtype while ignoring variance?) | ||
SubtypeKind = Tuple[bool, ...] | ||
SubtypeKind: TypeAlias = Tuple[bool, ...] |
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.
SubtypeKind: TypeAlias = Tuple[bool, ...] | |
SubtypeKind: _TypeAlias = Tuple[bool, ...] |
mypy/typestate.py
Outdated
|
||
# A cache that keeps track of whether the given TypeInfo is a part of a particular | ||
# subtype relationship | ||
SubtypeCache = Dict[TypeInfo, Dict[SubtypeKind, Set[SubtypeRelationship]]] | ||
SubtypeCache: TypeAlias = Dict[TypeInfo, Dict[SubtypeKind, Set[SubtypeRelationship]]] |
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.
SubtypeCache: TypeAlias = Dict[TypeInfo, Dict[SubtypeKind, Set[SubtypeRelationship]]] | |
SubtypeCache: _TypeAlias = Dict[TypeInfo, Dict[SubtypeKind, Set[SubtypeRelationship]]] |
What if we rename |
We already have both a TypeAlias and a TypeAliasType + might be a breaking change for plugins. I'm open to it, but I'm struggling to come up with names that work. Maybe we should just rewrite all of mypy in TypeScript? ;-) |
Thanks! I've missed a couple. Now it should be ready!
I am planning to do it in Rust 😉 |
Thank you! |
This is more an experiment to see if newly added
TypeAlias
in our own code is supported.Refs #11305