question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Annotate `is_bearable` to return TypeGuard

See original GitHub issue

Looks like TypeGuard is suitable as a return type for is_bearable for it to support mypy. I haven’t tested. Maybe it does support mypy already?

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:1
  • Comments:11 (7 by maintainers)

github_iconTop GitHub Comments

3reactions
leyceccommented, Sep 8, 2022

Resolved by db2b98e. Thanks so much for the gentle reminder. I’d gradually forgotten about PEP 647 and typing.TypeGuard, because it’s only supported only Python ≥ 3.10. But have no fear! Python can do anything that it probably shouldn’t, so we forcibly made this work by transparently falling back to bool under Python < 3.10. Dynamically interpreted languages for the win.

Unrelatedly, it turns out our ANSI stripping routine was indeed busted. I’m unclear why exactly that blew up tonight – but it did, spectacularly. But have no fear! I forcibly made that work too by removing a single excessive backslash. If you’re curious, here’s the working version (…which now resides in beartype._util.text.utiltextcolour, just because):

def strip_text_ansi(text: str) -> str:
    # Glory be to the one liner that you are about to read.
    return sub(r'\033[^m]*m', '', text)

\o/

2reactions
rsoklcommented, Sep 11, 2022

“Type checkers are special cased to always know what typing_extensions is even if it isn’t installed (it’s treated like part of the standard library)[^1]”. The following should work:

if typing.TYPE_CHECKING:
    from typing_extensions import TypeGuard
else:
    try:
        from typing import TypeGuard
    except ImportError:
        try:
            from typing_extensions import TypeGuard
        except ImportError:
            TypeGuard = bool

This should simplify the code considerably I believe 😄 !

[^1]: A quote from a mypy core dev

Read more comments on GitHub >

github_iconTop Results From Across the Web

Is it impossible to have a function return a user-defined type ...
Therefore, my suggestion would be to change your createFilter() so that the actual type guard function has a return type annotation:
Read more >
Narrowing types with TypeGuard in Python - Redowan Delowar
When TypeGuard is used to annotate the return type of a function or method that accepts at least one parameter, that function or...
Read more >
User guide — Typeguard 2.13.3 documentation - Read the Docs
If the function is called with incompatible types, or check_return_type() is used and the return value does not match the return type annotation, ......
Read more >
Type guard annotations don't work in jsdoc #25127 - GitHub
Actual behavior: No narrowing, and Entry.isInit 's return type is just boolean.
Read more >
PEP 647 – User-Defined Type Guards
TypeGuard is a special form that accepts a single type argument. It is used to annotate the return type of a user-defined type...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found