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.

typing.ContextManager --- warned against but unactionable?

See original GitHub issue

Hi @leycec , sorry to bother you again.

I am one to treat warnings as errors, and thus PEP 484/585 require me to make migrations of the form

from typing import Hashable

to

from collections.abc import Hashable

However, there appears not to be an analogue of typing.ContextManager. Accordingly, what do I do in this situation?

Here’s a minimal example:

from contextlib import contextmanager
from collections.abc import Iterator
from typing import ContextManager
from warnings import filterwarnings, simplefilter
from beartype import beartype

filterwarnings("error")


@contextmanager
@beartype
def foo() -> Iterator[None]:
    yield


f = foo()
print("\n\n\nfoo is correctly typed: {}\n\n\n".format(f))


@beartype
def bar(cm: ContextManager[None]) -> None:
    pass


bar(f)
print("did not get here")

resulting in:

> python foo.py



foo is correctly typed: <contextlib._GeneratorContextManager object at 0x7f3104e4d3a0>



Traceback (most recent call last):
   <snip>
    warn(
beartype.roar.BeartypeDecorHintPep585DeprecationWarning: PEP 484 type hint typing.ContextManag
er[NoneType] deprecated by PEP 585 scheduled for removal in the first Python version released
after October 5th, 2025. To resolve this, either drop Python < 3.9 support and globally replac
e this hint by the equivalent PEP 585 type hint (e.g., "typing.List[int]" by "list[int]") or s
ee this discussion topic for saner and more portable solutions:
    https://github.com/beartype/beartype#pep-484-deprecations

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
dycwcommented, Dec 9, 2021

Ahh, thanks guys for the help. I could’ve sworn I did a poor man’s grep (a.k.a., Ctrl-F) for ContextManager, but somehow that went wrong. In any case, onward with the @beartypeing!

> rg "@beartype" --stats

2360 matches
2360 matched lines
193 files contained matches
3731 files searched
49746 bytes printed
101142793 bytes searched
0.053398 seconds spent searching
0.031693 seconds
1reaction
leyceccommented, Dec 8, 2021

…sniped by Spence. Remind me never to foolishly engage in a vicious eBay bidding war over Portal memorabilia with you, Spence. That is the day I would surely lose everything.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Python type hints and context managers - mypy - Stack Overflow
PEP-585 mentions that typing.ContextManager is deprecated in favor of contextlib.AbstractContextManager . But it does not say it's the right ...
Read more >
Unable to use typing.ContextManager as return type #476
Describe the bug I am unable to use typing.ContextManager as the return type of a function decorated with contextlib.contextmanager.
Read more >
Python Type Hints - How to Type a Context Manager
Python's context manager protocol has only two methods, with straightforward types. But when it comes to adding accurate type hints to a ...
Read more >
contextlib — Utilities for with-statement contexts ... - Python Docs
This module provides utilities for common tasks involving the with statement. For more information see also Context Manager Types and With Statement Context ......
Read more >
Create launch-node.py · 0465f83c48 - windmill-ops - OpenDev: Free ...
There is a fair bit of assumptions here, but this is a good first start. ... This unactionable warning does not need to...
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