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

Typehints #11

Open
ramarivera opened this issue Oct 13, 2022 · 1 comment · May be fixed by #13
Open

Typehints #11

ramarivera opened this issue Oct 13, 2022 · 1 comment · May be fixed by #13

Comments

@ramarivera
Copy link

Hey, just stumbled on this library when a friend recommended it to me.

I like the syntax sugar, but I noticed it doesn't provide typehints, so the code I would save with this I need to spend doing cast .

Is that by design / for a particular reason? If not, would you accept a PR adding them?

@LEv145
Copy link

LEv145 commented Dec 23, 2022

I think the problem is in the value argument of the maybe function
https://github.com/ekampf/pymaybe/blob/master/pymaybe/__init__.py#L569-L570

Typing linters don't understand isinstance, so they warn about a possible error:

>>> maybe(Maybe()).log
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'Maybe' object has no attribute 'log'

image

Сan use @overload to fix it: https://docs.python.org/3/library/typing.html#typing.overload

@overload
def maybe(value: Maybe) -> Maybe:   # But such code can also be broken if `Maybe` is a child element of object
    ...  
@overload
def maybe(value: None) -> Nothing:
    ...
@overload
def maybe(value) -> Something:
    ...

Therefore, it is more logical to remove these lines or make a function without them:

if isinstance(value, Maybe):
   return value

LEv145 added a commit to LEv145/pymaybe that referenced this issue Dec 23, 2022
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

Successfully merging a pull request may close this issue.

2 participants