pytest-asyncio icon indicating copy to clipboard operation
pytest-asyncio copied to clipboard

Decorator type signatures not recognized by pyright

Open charles-dyfis-net opened this issue 1 year ago • 6 comments

...and according to pyright upstream, this is a bug in mypy, rather than in pyright.

Type-checking pytest-asyncio itself yields a great many errors, starting with:

pytest-asyncio/pytest_asyncio/plugin.py:57:60 - error: Type variable "_R" has no meaning in this context (reportGeneralTypeIssues)
pytest-asyncio/pytest_asyncio/plugin.py:57:36 - error: TypeVar bound type cannot be generic
pytest-asyncio/pytest_asyncio/plugin.py:60:65 - error: Type variable "_R" has no meaning in this context (reportGeneralTypeIssues)
pytest-asyncio/pytest_asyncio/plugin.py:60:37 - error: TypeVar bound type cannot be generic

...and type-checking a project that uses pytest-asyncio's decorators with reportUntypedFunctionDecorator set in pyright's configuration:

error: Untyped function decorator obscures type of function; ignoring decorator (reportUntypedFunctionDecorator)

I suspect that the option of either passing the function to be used as a fixture as a direct argument to fixture() or returning a function that acts as a proper decorator adds complexity here. If it's not possible to be pyright-friendly with the existing calling convention flexibility, might it make sense to offer a less-flexible, more explicitly-typed alternative?

charles-dyfis-net avatar Dec 28 '23 01:12 charles-dyfis-net

Thanks. Pytest-asyncio is currently type checked with an outdated version of mypy. There's an issue to update the type checker: #596

I assume it would be helpful to also type check with pyright in the CI.

Since pytest-asyncio v0.23 has functional issues that break some users' test suites, the typing issue you describe is not at the top of my priorities list at the moment. I hope you understand!

seifertm avatar Jan 01 '24 14:01 seifertm

@charles-dyfis-net pytest-asyncio-0.23.5a0 fixed some typing issues and removed the unbound type variable _R in the process. Can you try the pre release and check if it resolves your issue?

seifertm avatar Feb 06 '24 19:02 seifertm

We're not running clean, but the specific issue this ticket was filed for is no longer present:

pytest-asyncio/pytest_asyncio/plugin.py
  pytest-asyncio/pytest_asyncio/plugin.py:401:13 - error: Argument of type "Node | None" cannot be assigned to parameter "parent" of type "Node" in function "from_parent"
    Type "Node | None" cannot be assigned to type "Node"
      "None" is incompatible with "Node" (reportGeneralTypeIssues)
  pytest-asyncio/pytest_asyncio/plugin.py:652:30 - error: Cannot access member "__original_collect" for type "Module"
    Member "__original_collect" is unknown (reportGeneralTypeIssues)
pytest-asyncio/tests/test_is_async_test.py
  pytest-asyncio/tests/test_is_async_test.py:77:8 - error: Operator "<" not supported for types "tuple[Literal[7], Literal[4], Literal[0]]" and "tuple[Literal[7], Literal[2]]" (reportGeneralTypeIssues)
  pytest-asyncio/tests/test_is_async_test.py:80:10 - error: Operator "<" not supported for types "tuple[Literal[7], Literal[4], Literal[0]]" and "tuple[Literal[8]]" (reportGeneralTypeIssues)
pytest-asyncio/tests/hypothesis/test_base.py
  pytest-asyncio/tests/hypothesis/test_base.py:8:24 - error: "given" is unknown import symbol (reportGeneralTypeIssues)
  pytest-asyncio/tests/hypothesis/test_base.py:8:31 - error: "strategies" is unknown import symbol (reportGeneralTypeIssues)
6 errors, 0 warnings, 0 informations 

charles-dyfis-net avatar Feb 06 '24 22:02 charles-dyfis-net

Thanks for checking.

Resolving this issue would make for suitable first contribution to pytest-asyncio. I don't necessarily mean you, but anyone who is interested in working on this.

If anyone wants to open a PR, I would expect:

  • A pyright pre-commit hook to be added to .pre-commit-config.yaml. This will run pyright on every commit and prevent this kind of issue in the future
  • the docs and tests folders to be excluded from the pyright type check (see configuration of the mypy pre-commit hook)
  • The two typing errors to be fixed

seifertm avatar Feb 09 '24 17:02 seifertm

Hello, I am a new programmer, and would like to contribute to this issue. This will be my first open-source contribution. Thank You!

RoddyCodes avatar Jun 11 '25 00:06 RoddyCodes

Hi @RoddyCodes, thanks for the initiative! We appreciate your contribution.

Let us know, if you get stuck or need any additional information.

seifertm avatar Jun 20 '25 08:06 seifertm

Unfortunately, there is an issue with pre-commit and pyright. https://github.com/RobertCraigie/pyright-python/issues/345

tjkuson avatar Jul 24 '25 17:07 tjkuson

Pytest-asyncio v1.2 is now fully compatible with pyright. We ensure this as part of the development process. Thanks to @tjkuson for the fix.

seifertm avatar Sep 12 '25 07:09 seifertm