Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.Sign up
get_type_hints() fails on Mark.__init__ #3635
To reproduce, run the following snippet:
import typing import _pytest.mark print(typing.get_type_hints(_pytest.mark.Mark.__init__))
This happens because of these type annotations:
It's probably a minor issue that has no effect on the normal operation of pytest, but it could trip up runtime annotation analysis tools.
If the codebase was Python 3 only, the fix would be trivial:
from typing import List, Dict ... @attr.s(frozen=True) class Mark(object): name = attr.ib(type=str) args = attr.ib(type=List[object]) kwargs = attr.ib(type=Dict[str, object])
But I'm not sure how to proceed in Python 2/3 codebase, adding the conditionals does not seem justified for such a tiny thing.
Thanks @Vlad-Shcherbina for the report!
So right now it seems the
@attr.s(frozen=True) class Mark(object): name = attr.ib(type=str) args = attr.ib() # type: List[object] kwargs = attr.ib() # type: Dict[str, object]
@RonnyPfannschmidt what do you think?
Perhaps @hynek has another suggestion?
Firstly to b clear, it’s not necessarily just documentation since it’s metadata that is attached to the attributes that can be used by others – others being mostly mypy nowadays:
import attr @attr.s class X: x = attr.ib(type=int) X("x")
But it Is a non-standard way of defining types nonetheless, so I would expect type annotations to work better. Mypy knows about type= because it has an attrs plugin – CPython proper is blissfully ignorant. :)
It's a single 2KLOC file that imports some standard modules that are likely in the import cache already, defines a number of classes, and creates a few objects. So probably not too expensive.
from time import time start = time() import typing print(time() - start) # ~0.01s on my machine
Thanks for the clarification.
I'm a little worried about adding
So much that they have made it 7 times faster in Python 3.7:
I would rather use just the comments instead of adding yet another dependency which slows down pytest startup. We can always just use the