pytest-asyncio
pytest-asyncio copied to clipboard
Decorator type signatures not recognized by pyright
...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?
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!
@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?
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
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
docsandtestsfolders to be excluded from the pyright type check (see configuration of themypypre-commit hook) - The two typing errors to be fixed
Hello, I am a new programmer, and would like to contribute to this issue. This will be my first open-source contribution. Thank You!
Hi @RoddyCodes, thanks for the initiative! We appreciate your contribution.
Let us know, if you get stuck or need any additional information.
Unfortunately, there is an issue with pre-commit and pyright. https://github.com/RobertCraigie/pyright-python/issues/345
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.