Skip to content

NonNullable type #614

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

Closed
sobolevn opened this issue Feb 4, 2019 · 1 comment
Closed

NonNullable type #614

sobolevn opened this issue Feb 4, 2019 · 1 comment

Comments

@sobolevn
Copy link
Member

sobolevn commented Feb 4, 2019

There is a use-case when you might explicitly annotate some types as NonNullable, consider this example:

def some(arg):
    if arg is None:
        return Error('Sorry!')
    return SomeClass(arg)

So, the stub for this file will be:

T = TypeVar('T')

@overload
def some(arg: None) -> Error[str]:
     ...

@overload 
def some(arg: T) -> SomeClass[T]:
     ...

It works fine, but the problem is with Optional[] type.

x: Optional[int]
reveal_type(some(x))  # => Some[Optional[int]]

And there's no way to express Union[Some[int], Error[str]] without NonNullable.
Here's how it can look like:

@overload
def some(arg: None) -> Error[str]:
     ...

@overload 
def some(arg: NonNullable[T]) -> SomeClass[T]:
     ...

@overload 
def some(arg: Optional[T]) -> Union[SomeClass[T], Error[str]]:
     ...

Real-life example: https://github.com/dry-python/returns/blob/edb8ae203df518393d9568c25e40e5e8413aaeb9/returns/maybe.pyi#L15-L24

After a conversation with @ilevkivskyi I have searched both typing and mypy repos and I have not found a similar proposal. So, I am opening it.

@srittau
Copy link
Collaborator

srittau commented Oct 3, 2021

This would be useful from time to time. The (later) issue #801 suggests a general Not type, so I'm closing this issue as a specialized case of the other issue.

@srittau srittau closed this as completed Oct 3, 2021
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

2 participants