-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
config: improve typing #7401
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
config: improve typing #7401
Conversation
@@ -17,6 +18,7 @@ | |||
"getfslineno", | |||
"getrawcode", | |||
"Traceback", | |||
"TracebackEntry", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TracebackEntry
is exposed through Traceback
so it should be visible too (though this is an internal package anyway).
@@ -213,7 +213,7 @@ def statement(self) -> "Source": | |||
return source.getstatement(self.lineno) | |||
|
|||
@property | |||
def path(self): | |||
def path(self) -> Union[py.path.local, str]: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code.path
has this annoying type due to some error path, we should fix it sometime...
self._conftestpath2mod = {} # type: Dict[Any, object] | ||
# State related to local conftest plugins. | ||
self._dirpath2confmods = {} # type: Dict[py.path.local, List[types.ModuleType]] | ||
self._conftestpath2mod = {} # type: Dict[Path, types.ModuleType] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually stores Path
and not what the previous comment said.
I removed the comments since they are redundant with the types.
@@ -636,7 +671,6 @@ def import_plugin(self, modname: str, consider_entry_points: bool = False) -> No | |||
assert isinstance(modname, str), ( | |||
"module name as text required, got %r" % modname | |||
) | |||
modname = str(modname) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not needed per assert above.
@@ -793,9 +831,9 @@ class InvocationParams: | |||
Plugins accessing ``InvocationParams`` must be aware of that. | |||
""" | |||
|
|||
args = attr.ib(converter=tuple) | |||
args = attr.ib(type=Tuple[str, ...], converter=_args_converter) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Explicit converter instead of tuple
needed just for the type annotation to work
if config.option.color == "yes": | ||
tw.hasmarkup = True | ||
if config.option.color == "no": | ||
tw.hasmarkup = False | ||
return tw | ||
|
||
|
||
def _strtobool(val): | ||
"""Convert a string representation of truth to true (1) or false (0). | ||
def _strtobool(val: str) -> bool: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed _strtobool
to return bool instead of 0/1, seems more sensible.
@@ -141,9 +141,14 @@ def _get_auto_indent(auto_indent_option: Union[int, str, bool, None]) -> int: | |||
|
|||
if auto_indent_option is None: | |||
return 0 | |||
elif type(auto_indent_option) is int: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mypy doesn't do type narrowing on type(...) is ..
, so I changed the code to use isinstance
instead. Then things need to move a bit because isinstance(True, int)
is true.
raise ConftestImportFailure(path, sys.exc_info()) from e | ||
except Exception as exc: | ||
assert exc.__traceback__ is not None | ||
exc_info = (type(exc), exc, exc.__traceback__) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is needed because sys.exc_info()
has types with BaseException
while we want just Exception
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks for the hardworking here!
src/_pytest/config/__init__.py
Outdated
@@ -668,7 +702,7 @@ def import_plugin(self, modname: str, consider_entry_points: bool = False) -> No | |||
self.register(mod, modname) | |||
|
|||
|
|||
def _get_plugin_specs_as_list(specs): | |||
def _get_plugin_specs_as_list(specs) -> List[str]: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm curious, don't we need to type specs
here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added another commit which does this now, but it also refactors the function a bit. I was thrown off by the ModuleType
check but found the reference issue.
This improves the typing around
_pytest.config
, and also makes pytest completely clean with regards to the py typings in pytest-dev/py#232.