-
Notifications
You must be signed in to change notification settings - Fork 258
Do we need an explicit cast() function? #15
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
Comments
Mypy treats def f(x: Sequence[int]) -> None:
y = x # type: List[int] # Error, since Sequence is not compatible with List However, this is okay: def f(x: Sequence[int]) -> None:
y = cast(List[int], x) # Fine Effectively, type annotations (comments) are type safe (or as type safe as anything in mypy), but casts can do arbitrary things and work around the restrictions of the type system. Also, foo(cast(List[int], x)) If we used a type annotation as a cast, this would require an extra local variable: tmp = x # type: List[int]
foo(tmp) In more complex cases the refactoring could be non-trivial (it could break the order of evaluation in a boolean expression, for example). |
@JukkaL, do you have non-toy examples where working around the type system with |
It's actually a bit tricky to give a short, realistic example where a cast is clearly useful. Here is one inspired by a similar example in mypy:
Every example with a cast can be rewritten to not need a cast, but when adapting existing dynamically typed code, introducing a cast is often the simplest way of getting a piece of code to type check. |
It's good enough for me. :-) I also like using cast() to initialize result variable with an empty container of the right type:
|
Yeah,
If you use a
|
Maybe we need different kinds of casts, like in C++? This makes me a little On Wednesday, January 7, 2015, Jukka Lehtosalo [email protected]
--Guido van Rossum (on iPad) |
How about:
|
Łukasz, I presume you're talking about errors from the static checker only. After Jukka's comments in #33 I think it's better to use comments to disable the type checker than with-statements. Doing runtime checks in cast() is madness anyway (it took me a little while to realize this, but there are too many ways it can fail incorrectly, modify the value by even looking at it, or waste a lot of time). |
Yes, those would be static checker errors only. Fixed in e2e6fc4 (advertises usage of @no_type_checks for routines and "# type: ignore" for lines). |
Oh, the core issue, whether we need Jukka, you were worried about runtime performance of cast(), the comment would resolve that. |
As mentioned earlier, a So this would be rejected by the type checker:
But this would be okay:
Cast should be an expression, since casts are often used within subexpressions. |
Decision: we'll add |
… On Sat, Nov 30, 2019, 12:37 PM Omry Yadan ***@***.***> wrote:
Did the cast operator made it to the PEP?
I have a use case where I think it will be handy but as far as I can tell
it's not supported in 3.7 or 3.8, and it's not clear from this task which
PEP this is about.
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#15?email_source=notifications&email_token=AAM4YSKJRWKMPAXBOEHV2WDQWKXH7A5CNFSM4AV5E4NKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEFQREWQ#issuecomment-560009818>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAM4YSOYFCUSGJUT4VHCAKTQWKXH7ANCNFSM4AV5E4NA>
.
|
1. Changed an outdated example to an original one from python/typing#15 (comment) 2. I've listed more type narrowing techniques, like `type(obj) is some_class` 3. Fixed several headers Co-authored-by: Terence Honles <[email protected]>
In mypy there's a cast(TYPE, EXPR) function. Do we still need this, given that we can do the same thing with
# type:
comments? (However, see issue #9.)The text was updated successfully, but these errors were encountered: