-
Notifications
You must be signed in to change notification settings - Fork 243
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
What to do about Undefined #81
Comments
Jukka, Andrey and myself reviewed some uses of
can usually be spelled effectively as follows:
|
OK, killed it in the pep-0484.txt draft. |
Should this be valid as well (we discussed it as an option):
This matches the way a lot of people write Python right now, and has the benefit of not needing to import anything from |
I like that, though we must be careful to explain the difference between using And please think about what it might become in 3.6 when we replace it with |
@JukkaL, @gvanrossum: sorry, reopening for the following reasons. I would personally argue that Jukka's last suggestion is dangerous because:
One valid way to solve this in a generic way would be to treat those types as optional and to support type promotion (e.g. "we know this is always an int after this assignment") in the type checker. I think Mypy already supports a form of this. Am I right @JukkaL? If Jukka's suggestion is to be documented in the PEP, I'd like a strong example why this is necessary. |
Honestly, I'd like: x = None # type: forward int Another idea could be: x = None # type: unknown
# that says that the type will be inferred later on
x = 7 # now x's type is int |
If you are thinking about adding |
This is what I originally proposed, @ilevkivskyi. It was dismissed on the basis that:
|
These are all good objections, @ambv. Anyway, I think that the possibility to declare a variable is not very crucial. Probably, type hints for defined variables (including in |
I see two issues that we should resolve if we don't support the
How will we define a variable with an arbitrary type in a stub? Consider this stub: from typing import IO
stream = ??? # type: IO[str] How would we declare # ugly but works:
stream = cast(IO[str], None)
# similar to above:
stream = cast(IO[str], ...)
# literal ellipsis would be similar to default arg values, but this needs a PEP
# change since this could be flagged as an error by type checkers:
stream = ... # type: IO[str] These alternatives aren't really reasonable, in my opinion: stream = IO() # type: IO[str] # error, IO is an ABC
stream = open('') # ouch, type might be more precise or less precise than IO[str],
# depending on the signature of the open built-in and level of type inference
# for example, it could be IO[Any] or _io.TextIOWrapper
def _dummy() -> IO[str]: pass
stream = _dummy() # ugly and verbose
It's a fairly common idiom to use
Now a type checker may complain about the assignent to
Second, we can add an explicit check to
Third, we can remove the assignment from the class body:
Fourth, we can infer type The third option is not safe, since some external code may check if the value of the I don't have a strong opinion about this. Maybe all of these should be okay, depending on the context? |
For (1) let's do
So yes let's add this to the PEP. For (2) I don't think the PEP needs to take a position (we don't have to provide guidance for every single idiom in existing Python code). |
There are many reasons to dislike
Undefined
. Unfortunately the alternatives have issues. We need to decide.The text was updated successfully, but these errors were encountered: