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

Idea: allow description of type-effect for functions that return bool #2357

Closed
elazarg opened this issue Oct 27, 2016 · 11 comments
Closed

Idea: allow description of type-effect for functions that return bool #2357

elazarg opened this issue Oct 27, 2016 · 11 comments

Comments

@elazarg
Copy link
Contributor

elazarg commented Oct 27, 2016

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:

@type_effect(true='x <: Callable')
def callable(x: object) -> bool: ...

Although the syntax is of secondary in this case.

@gvanrossum
Copy link
Member

gvanrossum commented Oct 27, 2016 via email

@elazarg
Copy link
Contributor Author

elazarg commented Oct 27, 2016

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.

@ddfisher
Copy link
Collaborator

ddfisher commented Oct 28, 2016

Another similar language feature we could emulate (as suggested by @FuegoFro) is contract annotations.

@elazarg
Copy link
Contributor Author

elazarg commented Oct 29, 2016

What is the difference between contracts and dependent types, as far as typing goes?

@elazarg
Copy link
Contributor Author

elazarg commented Nov 9, 2016

This feature will also be helpful for equality, since we can assume that if x==y then x and y have the same type. So mypy can infer more precise types when a branch is taken, and emit a warning when the types of x and y are incompatible (i.e. neither can be downcast to the other).

@gvanrossum
Copy link
Member

But it's easy to override __eq__ so that x and y of different type can still be equal, and many classes use this.

@JukkaL
Copy link
Collaborator

JukkaL commented Nov 9, 2016

For example, decimal.Decimal(1) == 1 is true.

@elazarg
Copy link
Contributor Author

elazarg commented Nov 9, 2016

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.

@JukkaL
Copy link
Collaborator

JukkaL commented Nov 9, 2016

But Decimal and int are not closely related at type level -- neither is a subtype of the other. The stubs for decimal use union types to specify that int and Decimal can be mixed in operations, but that's about it.

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 __add__.

User-defined classes can define __eq__ in completely ad-hoc ways, and they probably at least occasionally do this.

@DustinWehr
Copy link

@JukkaL you could close this in favour of #5206, which though newer has more discussion.

@JukkaL
Copy link
Collaborator

JukkaL commented Jan 9, 2020

Closing in favor of #5206.

@JukkaL JukkaL closed this as completed Jan 9, 2020
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

5 participants