-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Literal types tracking issue #5935
Comments
The plan looks great! I'm looking forward to being able to use literal types. |
This diff adds a 'LiteralType' class to types.py and adds a corresponding stub method to all of the type visitors. Most of these stub methods just throw a 'NotImplementedError', though I did pencil in a few implementations that looked obvious. The reason I'm doing this now instead of later is because I want to start by getting these kind of big, gross, multi-file changes out of the way early and try and have any subsequent pull requests be more focused. I also want to formally confirm that the correct approach here *is* to create a new 'LiteralType' class before I start on the rest of the implementation work. (Other possible approaches would be to tack on some extra attribute to 'Instance', or make 'LiteralType' a subclass of 'Instance'.) No tests, sorry. My plan is to work on modifying the parsing and semantic analysis steps next before returning back to these the unimplemented methods and add tests then -- here's the tracking issue for these TODOs: #5935 Tests shouldn't be necessary, in any case: everything I added just now is basically dead code.
I think this idea already appeared somewhere, but I think it makes sense to add this to this list: custom type-guards (like in TypeScript): @overload
def is_spec(arg: Union[int, List[int]]) -> Literal[True]: ...
@overload
def is_spec(arg: object) -> bool: ...
def is_spec(arg):
# some complex logic here
...
x: Union[int, str]
if is_spec(x):
x += 42 essentially we just nee to teach binder how to interact with literals. |
@Michael0x2a I propose to use #5206 to track the type-guards idea (literals + binder). |
There seem to be 3 relatively simple tasks left:
the last two in particular should be good first issues if I'm not mistaken. |
I don't know that we need to error for the second one, and I feel we shouldn't disallow erroring for the third one (or any bound to a final type). I can't see what specific tests we'd want to add for the first one. |
This issue tracks TODOs for literal types.
Core work
Parsing/semantic analysis follow-up tasks
from __future__ import unicode_literals
(Add support for byte and unicode Literal strings #6087)Error messages
Literal[1 + 2]
. This will probably involve moving all invalid type checking into the semantic analysis layer? (Improve error messages related to literal types #6149)Implementing and testing type visitors
Implement and write tests that exercise the following visitors. (Introduced by #5934)
The following have preliminary implementations, but are missing associated tests:
Other checks to add in the type-checking layer
Type[Literal[4]]
Type[Literal[4]]
asint
instead -- this might make certain TypeVar shenanigans more smooth. Need to discuss.x: Literal[3]
, doingtype(x)
ought to return a value with typeType[int]
, not justint
.Literal[...]
inisinstance
andissubclass
checks (Prohibit some illegal uses of Literal #6034)Literal
works as expected when used in generics (Add tests for literals and generics #6035)TypeVar('T', bound=Literal[4])
? Need to discuss first."Extra" features to add
--disallow-any-expr
?Final
(Add interactions between Literal and Final #6081)Literal[True]
andLiteral[False]
(see Treat bool as equivalent to Literal[True, False] to allow better type inference #6113)Things to do before mypy 0.660 is released if literal types aren't going to make it
Other things
open
, about 610 of them had 2 or more arguments. We inferred the correct type for about 560 of those. About another 20 of these were passing in string variable arguments as the mode. The remaining 30 require a little more investigation -- some people were passing in**kwargs
or**args
in as the argument, etc...Things that have been deferred to after this project is completed
Revealed type is 'Literal['foo']'
). We should standardize on a specific quoting scheme + avoid this weird "nesting" issue.Literal[3]()
(andFinal[int]()
andProtocol[blah]()
...)The text was updated successfully, but these errors were encountered: