Skip to content
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

Safe cast (upcast) #5756

Open
JukkaL opened this issue Oct 9, 2018 · 4 comments
Open

Safe cast (upcast) #5756

JukkaL opened this issue Oct 9, 2018 · 4 comments

Comments

@JukkaL
Copy link
Collaborator

JukkaL commented Oct 9, 2018

Sometimes I want to give a hint to mypy about a type of an expression. cast() works here, but it's inherently unsafe. If I make a mistake, my program may crash at runtime. If we had a "safe cast" operation instead that would only allow casts that can't introduce runtime failures, mypy could catch some additional errors, and the use of cast() could be reduced a bit.

Here's an example derived from an example in mypy of where it would be helpful:

from typing import List, Optional
from mypy_extensions import safe_cast

def f(x: List[Optional[int]], n: int) -> List[Optional[int]]:
    # Mypy doesn't allow + for List[None] and List[Optional[int]], so we need a cast
    return safe_cast(List[Optional[int]], [None]) * n + x

In this case an alternative would be to improve type checking or type inference in mypy, but there will likely always be cases where mypy won't get thing right, and being able to perform a cast would be helpful.

Semantics of safe_cast:

  • Provides a type context for the expression (useful in the above example)
  • Requires that the expression has a type that is a "proper subtype" (mypy terminology) of the target type

At runtime safe_cast just returns the second argument, similar to cast.

More generally, if we had safe_cast, we could plausibly use binder on initial assignment (#2008), since safe_cast would be a simple workaround for cases where using binder is not desirable. Example where this would make a difference:

x: Union[int, str] = 0
reveal_type(x)  # currently Union[int, str], but perhaps should be 'int'

Now if the revealed type was int, we could use safe_cast to get the original behavior without introducing type unsafety:

x = safe_cast(Union[int, str], 0)  # Type safe!
reveal_type(x)  # Union[int, str]

Safe cast has these benefits over cast():

  • Prevents accidentally introducing an unsafe cast
  • Helps catch issues where a previously safe cast silently becomes unsafe because some types have changed
  • Makes things obvious for a reader or reviewer -- an unsafe cast always needs some care, as it could hide a real issue

Related issues: #5750 (example where this could be helpful), #5687 (safer downcasts)

@gvanrossum
Copy link
Member

Remind us what "proper subtype" means and how it differs from "subtype"?

@davidfstr
Copy link
Contributor

FWIW, I believe it would be trivial to implement safe_cast in pure Python if support for TypeForm were added:

def safe_cast(form: TypeForm[T], value: T) -> T
    return value

@septatrix
Copy link
Contributor

FWIW, I believe it would be trivial to implement safe_cast in pure Python if support for TypeForm were added:

I definitely agree. Currently it is not possible to implement a custom variant of cast without developing plugins for mypy.
In our codebase we have three variants of cast: cast_{check,is,affirm} which return a bool, Tuple[Optional[T], List[Exception] and T (raising an exception on mismatch) respectively. Due to the limitations of Type we have to create separate methods for generics like cast_{check,is,affirm}_optional or cast_{check,is,affirm}_list.

The reason to have these functions is that they are not noops but also perform further analysis on the structure and values of the types (e.g. many of them are dicts but not static enought to use typed dicts and some others like EMail etc are just a restricted string).

@JukkaL
Copy link
Collaborator Author

JukkaL commented Jan 6, 2021

Remind us what "proper subtype" means and how it differs from "subtype"?

It's mypy jargon. It's basically "standard subtype" with no special rules for Any. For example, bool is a proper subtype of int, and List[int] is a proper subtype of Sequence[object].

Plain "subtype" recognizes Any as special, so that List[Any] is a subtype of List[int], and vice versa. (However, List[Any] is not a proper subtype of List[int], and neither is List[int] a proper subtype of List[Any].)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants