Skip to content

Doesn't support hinting #34

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
matanbanin opened this issue Jun 23, 2020 · 10 comments
Closed

Doesn't support hinting #34

matanbanin opened this issue Jun 23, 2020 · 10 comments
Labels
enhancement New feature or request

Comments

@matanbanin
Copy link

Which is pretty crucial on a wrap of an object, i couldn't use it that way...

@cbefus
Copy link
Collaborator

cbefus commented Jun 23, 2020

Type hinting was introduced in python 3.5 and continues to evolve. Due to our goal of supporting as many python versions as possible, type hinting has taken a backseat. We will continue to watch for a way to backport type hints of generics through all supported versions of python and reevaluate.

That being said, this is open source and we are very open to contribution, so feel free to open a PR.

@cbefus cbefus added the enhancement New feature or request label Jun 23, 2020
@matanbanin
Copy link
Author

How can i implement it on python>=3.5 and that it will still be backward compatible?

@cbefus cbefus linked a pull request Jun 23, 2020 that will close this issue
@cbefus
Copy link
Collaborator

cbefus commented Jun 23, 2020

@matanbanin Yes that is my struggle too! If you look at the PR I made above I tried adding them in a way that works for all versions of Python but the hinting doesnt seem 100% correct. Feel free to pull that branch as a initial starting point!

@matanbanin
Copy link
Author

I can't create a new branch, so i'll post my code here optional.py

It seems to work on my python, is this code compatible on older versions?

from typing import Any, TypeVar, Generic

from optional.abstract_optional import AbstractOptional
from .nothing import Nothing
from .something import Something

T = TypeVar('T')

class Optional(Generic[T]):
@classmethod
def of(cls, thing: T = None) -> AbstractOptional[T]:
return Nothing(cls) if thing is None else Something(thing, cls)

@classmethod
def empty(cls):
    # type: () -> 'Nothing'
    return Nothing(cls)

int = Optional.of(1)
int.get()

@cbefus
Copy link
Collaborator

cbefus commented Jun 26, 2020

Unfortunately, what you did above is not completely backwards compatible. Pre 3.5 versions of python only accept the comment style of hints so your line:

def of(cls, thing: T = None) -> AbstractOptional[T]:

would cause problems due to both the : T = None and the -> AbstractOptional[T].

I made a branch called Typing which is close. @dpassen might get a chance to review it this weekend.

@matanbanin
Copy link
Author

from typing import Any, TypeVar, Generic

from optional.abstract_optional import AbstractOptional
from .nothing import Nothing
from .something import Something

T = TypeVar('T')

class Optional(Generic[T]):
@classmethod
def of(cls, thing= None):
# type: (T) -> AbstractOptional[T]
return Nothing(cls) if thing is None else Something(thing, cls)

@classmethod
def empty(cls):
    # type: () -> 'Nothing'
    return Nothing(cls)

int = Optional.of(1)
int.get()

And this?

@matanbanin
Copy link
Author

?

@cbefus
Copy link
Collaborator

cbefus commented Mar 13, 2024

@dpassen this is solved by new version right?

@dpassen
Copy link
Collaborator

dpassen commented Mar 13, 2024

Using pyright language server, this is what I see using the lib:
image

@cbefus
Copy link
Collaborator

cbefus commented Mar 13, 2024

lgtm closing

@cbefus cbefus closed this as completed Mar 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants