-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Idea: allow description of type-effect for functions that return bool #2357
Comments
I think this idea is similar to what TypeScript calls "User-Defined Type
Guards" (see
https://www.typescriptlang.org/docs/handbook/advanced-types.html).
|
Nice, I didn't notice that. (Returning a type predicate is a cool idea, but the closest I can think of to type predicates in Python is a string :) ) Thinking about it this way, I must note that a type guard is a dependently-typed function, as hinted by the fact that it's hard to describe with eagerly-evaluated annotations: the type of the result (an assertion on x) depends on the value of x. But I think it should not be so hard to implement. |
Another similar language feature we could emulate (as suggested by @FuegoFro) is contract annotations. |
What is the difference between contracts and dependent types, as far as typing goes? |
This feature will also be helpful for equality, since we can assume that if |
But it's easy to override |
For example, |
Indeed. But correct me if I'm wrong, it's mainly among related classes, so if I know the class is numeric, I will know its signature only promises that the other is also numeric. And if I don't know the actual class, then probably no harm done since I will not "downcast" the types too much. |
But There is really no concept corresponding to 'numeric' in the type system. Such a thing would likely be quite tricky to define in a useful/sound way, because of complications around subtyping and the semantics of operator methods such as User-defined classes can define |
Closing in favor of #5206. |
I often encounter situations where I would like to extract some test into a function, but then mypy will not understand the implication on the types of the arguments. This goes against the intention that type checking will guide the programmer towards writing more readable code.
Similar case is functions like callable as in #1973, which requires hard coding so that mypy will understand.
I suggest a
@type_effect
decorator, possibly something along these lines:Although the syntax is of secondary in this case.
The text was updated successfully, but these errors were encountered: