Skip to content

An Infer annotation #298

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

Open
KotlinIsland opened this issue May 17, 2022 · 5 comments
Open

An Infer annotation #298

KotlinIsland opened this issue May 17, 2022 · 5 comments
Labels

Comments

@KotlinIsland
Copy link
Owner

Useful to resolve default_return vs infer return type.

def foo(a) -> Infer:
    return 1

reveal_type(foo) # def(a: Untyped) -> Literal[1]
@Zeckie
Copy link

Zeckie commented Jun 11, 2022

What contexts can this be used in? Is that just for the return type of methods / functions that contain a return?

Should it be able to be combined with other types, such as List[Infer] or Infer | None?

Should it be able to infer TypeVars, in cases like:

def foo(a) -> Infer:
   return a

being the same as

from typing import TypeVar
T = TypeVar("T")
def foo(a: T) -> T:
   return a

or would that only work if a was already typed as T?

And is inferring the return type it is a Literal (in your example) going to cause issues similar to those discussed in python/typeshed#7258 ?

@DetachHead
Copy link
Collaborator

it's not the same as using a TypeVar because that would allow you to narrow the return type when calling the function, which isn't valid:

def foo() -> T: 
    result: int = 2
    return result

bar: Literal[1] = foo()

which is why you get this error:

Incompatible return value type (got "int", expected "T")

@Zeckie
Copy link

Zeckie commented Jun 11, 2022

In my example, the parameter a was returned, so narrowing the return type would be valid.

@KotlinIsland
Copy link
Owner Author

KotlinIsland commented Jun 11, 2022

def foo(a) -> Infer:
   return a

Potentially something like this could be accepted where the type checker figures out that a is implicitly polymorphic, but I think that sounds like a completely different feature to what this issue is tracking. Instead you would just get an error that a is missing an annotation and the function def would end up being def (a: Untyped) -> Untyped.

@DetachHead
Copy link
Collaborator

could this also be used in conditional types like in typescript?

type Foo[T] = R if T extends Callable[[], Infer[R]] else Never

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

No branches or pull requests

3 participants