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

wip: Add is_ok() and is_err() typing.TypeGuard shorthands #72

Closed
wants to merge 1 commit into from

Conversation

wbolster
Copy link
Member

@wbolster wbolster commented Nov 1, 2021

See #69

@wbolster wbolster self-assigned this Nov 1, 2021
@wbolster
Copy link
Member Author

wbolster commented Nov 1, 2021

@erencank
Copy link

blocked by

for the time being

Do you have any updates now that the issue was successfully closed, as I would want this to be implemented as well?

@wbolster
Copy link
Member Author

this indeed works these days using

$ mypy --version
mypy 0.971 (compiled: yes)

the ci failure on

E   AttributeError: module 'flake8.options.config' has no attribute 'ConfigFileFinder'

is due to tholo/pytest-flake8#87

@wbolster
Copy link
Member Author

i've updated and rebased and simplified this (draft) PR now that upstream mypy issues have been resolved

@wbolster
Copy link
Member Author

E   AttributeError: module 'flake8.options.config' has no attribute 'ConfigFileFinder'

fixed via #101

def test_is_ok_is_err() -> None:
"""The module-level check functions work."""
o: Result[str, int] = Ok('yay')
n: Result[str, int] = Err(100)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm using pyright and it's reporting these as Ok instead of Result. A simple fix is to define a identify function that makes the typechecker not report these as the literal types,

    def passthrough(o: Result[str, int]) -> Result[str, int]:
        return o

    o = passthrough(Ok('yay'))
    n = passthrough(Err(100))

Without this passthrough function, you don't get proper type checking in this case,

    """The module-level type guard shorthands work."""
    def passthrough(o: Result[str, int]) -> Result[str, int]:
        return o

    o = passthrough(Ok('yay'))
    n = passthrough(Err(100))

    # Type checkers supporting typing.TypeGuard should accept the code below:
    if not is_ok(o):
        _ = o.value + ""  # works only for str
              ^^^^^^^^^^^
              Operator "+" not supported for types "str | int" and "Literal['']" 
    if not is_err(n):
        _ = n.value + 0  # works only for int
              ^^^^^^^^^^^
              Operator "+" not supported for types "str | int" and "Literal[0]"

@francium
Copy link
Member

francium commented Aug 2, 2023

Closing in favor of 135

@francium francium closed this Aug 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants