Skip to content

Context manager contextlib.nullcontext causes diagnostic #460

@behrenhoff

Description

@behrenhoff

Summary

Consider the following simple code:

from contextlib import nullcontext


def foo() -> None:
    with nullcontext():
        print("ok")

Then:

$ ty --version
ty 0.0.1-alpha.5

$ ty check ty-test.py                    
WARN ty is pre-release software and not ready for production use. Expect to encounter bugs, missing features, and fatal errors.
error[no-matching-overload]: No overload of bound method `__init__` matches arguments
 --> ty-test.py:5:10
  |
4 | def foo() -> None:
5 |     with nullcontext():
  |          ^^^^^^^^^^^^^
6 |         print("ok")
  |
info: First overload defined here
   --> stdlib/contextlib.pyi:188:13
    |
186 |         enter_result: _T
187 |         @overload
188 |         def __init__(self: nullcontext[None], enter_result: None = None) -> None: ...
    |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
189 |         @overload
190 |         def __init__(self: nullcontext[_T], enter_result: _T) -> None: ...  # pyright: ignore[reportInvalidTypeVarUse]  #11780
    |
info: Possible overloads for bound method `__init__`:
info:   (self: nullcontext[None], enter_result: None = None) -> None
info:   (self: nullcontext[_T], enter_result: _T) -> None
info: rule `no-matching-overload` is enabled by default

Found 1 diagnostic

I am not exactly sure what is wrong here but the code above should not produce a diagnostic. It seems it has to do with contextlib.nullcontext as the disagnostic does not appear when using pytest.raises instead.

Version

0.0.1-alpha.5

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions