Type annotate fixtures.py & related#6736
Conversation
afad3d8 to
9bda23f
Compare
| # we arrive here because of a dynamic call to | ||
| # getfixturevalue(argname) usage which was naturally | ||
| # not known at parsing/collection time | ||
| assert self._pyfuncitem.parent is not None |
There was a problem hiding this comment.
This is immediately dereferenced below so should be fine.
| """ | ||
| return self._get_active_fixturedef(argname).cached_result[0] | ||
| fixturedef = self._get_active_fixturedef(argname) | ||
| assert fixturedef.cached_result is not None |
There was a problem hiding this comment.
Immediately dereferenced below.
| values.reverse() | ||
| return values | ||
| values.append(fixturedef) | ||
| assert isinstance(current, SubRequest) |
There was a problem hiding this comment.
This attribute is only set on SubRequest.
There was a problem hiding this comment.
That would make a helpful in-code comment for future maintainers 😄
There was a problem hiding this comment.
I don't know, once the assert is in, the implication is reversed -- the access is legal because of the assert, not the other way :)
|
|
||
|
|
||
| scopes = "session package module class function".split() | ||
| scopes = ["session", "package", "module", "class", "function"] # type: List[_Scope] |
There was a problem hiding this comment.
Changed to make the type annotation work (_Scope is a Literal).
| """Look up the index of ``scope`` and raise a descriptive value error | ||
| if not defined. | ||
| """ | ||
| strscopes = scopes # type: Sequence[str] |
There was a problem hiding this comment.
Just a hack to expand the type of scopes to allow checking against an str rather than _Scope.
| exceptions.append(sys.exc_info()) | ||
| if exceptions: | ||
| _, val, tb = exceptions[0] | ||
| assert val is not None |
There was a problem hiding this comment.
Dereferenced just below.
| # display it now and during the teardown (in .finish()). | ||
| if fixturedef.ids: | ||
| if callable(fixturedef.ids): | ||
| fixturedef.cached_param = fixturedef.ids(request.param) |
There was a problem hiding this comment.
Changed so I only need to ignore once.
There was a problem hiding this comment.
Maybe a helper method FixtureDef.set_cached_param would be appropriate? Though I can see an argument that it's not worth the trouble when we have to maintain the current option for backwards-compatibility.
There was a problem hiding this comment.
Yeah, I'm not sure it's worth it.
9bda23f to
3856200
Compare
3856200 to
c0c4ca9
Compare
| # display it now and during the teardown (in .finish()). | ||
| if fixturedef.ids: | ||
| if callable(fixturedef.ids): | ||
| fixturedef.cached_param = fixturedef.ids(request.param) |
There was a problem hiding this comment.
Maybe a helper method FixtureDef.set_cached_param would be appropriate? Though I can see an argument that it's not worth the trouble when we have to maintain the current option for backwards-compatibility.
|
|
||
| def add_funcarg_pseudo_fixture_def(collector, metafunc, fixturemanager): | ||
| def add_funcarg_pseudo_fixture_def( | ||
| collector, metafunc: "Metafunc", fixturemanager: "FixtureManager" |
There was a problem hiding this comment.
Worth annotating collector? AFAICT it's an Optional[Type[nodes.Node]].
There was a problem hiding this comment.
When I'm not sure of something, and too lazy to check/verify, I leave it unannotated. Usually some later PR adds it :)
| values.reverse() | ||
| return values | ||
| values.append(fixturedef) | ||
| assert isinstance(current, SubRequest) |
There was a problem hiding this comment.
That would make a helpful in-code comment for future maintainers 😄
| self, | ||
| request: "FixtureRequest", | ||
| scope: "_Scope", | ||
| param, |
There was a problem hiding this comment.
IIRC, this one can also be NOTSET, and I'd like to distinguish it in the type annotation from just an object (although object subsumes everything) just for clarity.
I think there is another PR that does that although I sort of lost track myself already 👣
|
|
||
|
|
||
| def _eval_scope_callable(scope_callable, fixture_name, config): | ||
| def _eval_scope_callable(scope_callable, fixture_name: str, config: Config) -> str: |
There was a problem hiding this comment.
scope_callable: Callable[..., str] - speccing keyword arguments is pretty fiddly, but even without the arguments specified this could catch a fair few issues.
There was a problem hiding this comment.
I prefer to leave this one for for later.
|
|
||
| def getfixtureclosure(self, fixturenames, parentnode, ignore_args=()): | ||
| def getfixtureclosure( | ||
| self, fixturenames: Tuple[str, ...], parentnode, ignore_args: Sequence[str] = () |
There was a problem hiding this comment.
I'm not sure, I prefer to leave this one.
c0c4ca9 to
cba62b6
Compare
|
|
||
| def add_funcarg_pseudo_fixture_def(collector, metafunc, fixturemanager): | ||
| def add_funcarg_pseudo_fixture_def( | ||
| collector, metafunc: "Metafunc", fixturemanager: "FixtureManager" |
There was a problem hiding this comment.
When I'm not sure of something, and too lazy to check/verify, I leave it unannotated. Usually some later PR adds it :)
| values.reverse() | ||
| return values | ||
| values.append(fixturedef) | ||
| assert isinstance(current, SubRequest) |
There was a problem hiding this comment.
I don't know, once the assert is in, the implication is reversed -- the access is legal because of the assert, not the other way :)
| self, | ||
| request: "FixtureRequest", | ||
| scope: "_Scope", | ||
| param, |
There was a problem hiding this comment.
IIRC, this one can also be NOTSET, and I'd like to distinguish it in the type annotation from just an object (although object subsumes everything) just for clarity.
I think there is another PR that does that although I sort of lost track myself already 👣
|
|
||
|
|
||
| def _eval_scope_callable(scope_callable, fixture_name, config): | ||
| def _eval_scope_callable(scope_callable, fixture_name: str, config: Config) -> str: |
There was a problem hiding this comment.
I prefer to leave this one for for later.
|
|
||
| def getfixtureclosure(self, fixturenames, parentnode, ignore_args=()): | ||
| def getfixtureclosure( | ||
| self, fixturenames: Tuple[str, ...], parentnode, ignore_args: Sequence[str] = () |
There was a problem hiding this comment.
I'm not sure, I prefer to leave this one.
| # display it now and during the teardown (in .finish()). | ||
| if fixturedef.ids: | ||
| if callable(fixturedef.ids): | ||
| fixturedef.cached_param = fixturedef.ids(request.param) |
There was a problem hiding this comment.
Yeah, I'm not sure it's worth it.
Extracted from #6717.
Depends/based on #6737.