-
Notifications
You must be signed in to change notification settings - Fork 242
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
Allow Ellipsis as annotation for inferred type #276
Comments
This kind of inference could be very costly -- you may have to trace
arbitrary many calls deep.
|
The checker could still give up and use
I don't have any insight into how practical that kind of thing would be to implement, though. |
|
Maybe we could still allow the second form (with Nick's addition about inference cost)? def g(x: int) -> ...:
return x
reveal_type(g(1)) # Revealed type is 'Any' |
I think this is something where we should first come up with an
experimental implementation (maybe a flag for mypy) before we decide to
modify PEP 484. (And what should get_type_hints() do?)
|
@gvanrossum |
BTW in PyCharm we just infer types for values that are missing (thus not conforming formally to PEP 484). |
Or use pytype to generate the type information. ;) |
This is an experimental features that allows explicitly annotating a type as "inferred." See python/typing#276. PiperOrigin-RevId: 420417222
Related example: import dataclasses
import typing
class Class:
def __init__(self, x):
self.x = x
@dataclasses.dataclass
class Dataclass:
x: ... # none of these work: object, ..., typing.Any, None, str | int
typing.reveal_type(Class("test").x) # str
typing.reveal_type(Dataclass("test").x) # ...
typing.reveal_type(Class(3).x) # int
typing.reveal_type(Dataclass(3).x) # ... Is there anything that can be substituted for There seems to be a tension between the type-hints-never-required philosophy and the way dataclasses work: PEP 526 – Syntax for Variable Annotations
|
Unless i missed your point, you can use regular generics in this case. |
Thanks. That approach works for this example, but has a few drawbacks:
from typing import reveal_type
from dataclasses import dataclass
class Class:
def __init__(self, x):
self.x = x
def f(self):
return self.x + 3
@dataclass
class Dataclass[T]:
x: T
def f(self):
return self.x + 3
# Operator "+" not supported for types "T@Dataclass" and "Literal[3]" from typing import reveal_type
from dataclasses import dataclass
class Class:
def __init__(self, x, y):
self.x = x
self.y = y
def f(self):
return self.x * self.y
@dataclass
class Dataclass[T, U]:
x: T
y: U
def f(self):
return self.x * self.y
# Operator "*" not supported for types "T@Dataclass" and "U@Dataclass" Fields might interact with each other and expressions inside the class in a way that gives rise to nontrivial type constraints that cannot be expressed via a simple generic. What I'd really like to do is to say nothing about the type of the field, and to let type inference/checking "pass through" it as much as possible, as if the annotation didn't exist, so to speak. |
PEP 484 says that if an annotation is missing then the type assumed to be
Any
However, it is not clear how to say to type checker that it should infer a missing type, rather than assume that it is
Any
. It was proposed by @ncoghlan to use Ellipsis for this purpose:I am opening this issue, so that this idea will not be forgotten.
The text was updated successfully, but these errors were encountered: