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

Use abstract types from numbers #7217

Closed
sobolevn opened this issue Feb 15, 2022 · 4 comments
Closed

Use abstract types from numbers #7217

sobolevn opened this issue Feb 15, 2022 · 4 comments

Comments

@sobolevn
Copy link
Member

sobolevn commented Feb 15, 2022

Right now types declared in numbers.pyi are not used.
This causes this problem:

from numbers import Real

def test(f: Real) -> Real:
    return f

test(1.32)
# error: Argument 1 to "test" has incompatible type "float"; expected "Real"

But, in runtime:

But, how can we type it? Options:

  1. Use a base class in builtins.pyi
  2. Use Real.register(float) in builtins.pyi
  3. Use Real.register(float) in numbers.pyi (as runtime does)
@srittau
Copy link
Collaborator

srittau commented Feb 15, 2022

I don't think we can really do anything in typeshed. This is best handled on the type checker level.

@hauntsaninja
Copy link
Collaborator

An approach like python/mypy#3186 (comment) could be feasible.

@srittau
Copy link
Collaborator

srittau commented Feb 15, 2022

A typing feature that allows stubs to register a type with another type, similar to what abcs provide at runtime, would be useful for those legacy features, though.

@AlexWaygood
Copy link
Member

I invested some energy a while back into seeing if you could do this, and I think the honest answer is that there's no way to make it work at typeshed. Mypy now gives a much more helpful error message if you try to use an abstract type from numbers these days, following python/mypy#15137.

Closing as per the rationale in python/mypy#3186 (comment)

@AlexWaygood AlexWaygood closed this as not planned Won't fix, can't repro, duplicate, stale Nov 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants