Skip to content

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

Merged
merged 2 commits into from
Jun 23, 2020
Merged

config: improve typing #7401

merged 2 commits into from
Jun 23, 2020

Conversation

bluetech
Copy link
Member

This improves the typing around _pytest.config, and also makes pytest completely clean with regards to the py typings in pytest-dev/py#232.

@@ -17,6 +18,7 @@
"getfslineno",
"getrawcode",
"Traceback",
"TracebackEntry",
Copy link
Member Author

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]:
Copy link
Member Author

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]
Copy link
Member Author

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)
Copy link
Member Author

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)
Copy link
Member Author

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:
Copy link
Member Author

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:
Copy link
Member Author

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__)
Copy link
Member Author

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.

Copy link
Member

@nicoddemus nicoddemus left a 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!

@@ -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]:
Copy link
Member

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?

Copy link
Member Author

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.

@bluetech bluetech merged commit 3624acb into pytest-dev:master Jun 23, 2020
@bluetech bluetech deleted the typing-config branch June 28, 2020 15:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants