-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Add type annotations to _pytest.compat and _pytest._code.code #6205
Conversation
Add some Python 3.8 type: ignores; all are already fixed in the next mypy release, so can be removed once we upgrade. Also move some flake8 ignores which seem to have changed places.
pytest doesn't support these PyPy versions anymore, so no need to have checks for them.
It doesn't help much IMO, just adds indirection and makes it harder to type.
The previous test was better in that it used fakes to test all of the real code paths. The problem with that is that it makes it impossible to simplify the code with `isinstance` checks. So let's just simulate the issue directly with a monkeypatch.
Source was previously iterable because it implements `__getitem__()`, which is apparently a thing from before `__iter__()` was introduced. To reduce mypy's and my own confusion, implement `__iter__()` directly.
b512b9c
to
51e3be2
Compare
@@ -60,7 +60,7 @@ def __getitem__(self, key: int) -> str: | |||
raise NotImplementedError() | |||
|
|||
@overload # noqa: F811 | |||
def __getitem__(self, key: slice) -> "Source": | |||
def __getitem__(self, key: slice) -> "Source": # noqa: F811 |
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.
There seems to be a discrepancy between where CI flake8 and my flake8 report this - on the decorator or the def
. Maybe because of Python 3.8? So for now I add it to both.
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.
Maybe because of Python 3.8?
Very likely, yes.
Isn't there a flake8 plugin to handle this automatically maybe?
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.
There seems to be some discussion regarding this here, I'm too lazy to read through it though...
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.
Awesome!
@@ -38,13 +46,12 @@ class Code: | |||
def __init__(self, rawcode) -> None: |
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.
Can we type the rawcode
arg 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.
Not really. I spent some time trying to type getrawcode
but left it aside for now.
@@ -134,7 +141,7 @@ def eval(self, code, **vars): | |||
f_locals.update(vars) | |||
return eval(code, self.f_globals, f_locals) | |||
|
|||
def exec_(self, code, **vars): | |||
def exec_(self, code, **vars) -> None: |
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.
Can the args be typed here also?
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.
They can be anything really AFAIK, but I'm not entirely sure, so I left it out.
src/_pytest/_code/code.py
Outdated
|
||
def __init__(self, tb, excinfo=None): | ||
def __init__( | ||
self, tb: Union[TracebackType, Iterable[TracebackEntry]], excinfo=None |
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.
Can excinfo
be typed?
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.
Yes, will add.
path=None, | ||
lineno: Optional[int] = None, | ||
firstlineno: Optional[int] = None, | ||
excludepath=None, |
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.
Can excludepath
be typed?
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.
It's can be a py.path
AFAIU, so I'm leaving it unannotated.
# Type ignored because mypy thinks the slice is a List[TracebackEntry] | ||
# instead of the Traceback subclass, even though we override __getitem__(). | ||
# Should figure out why it does that. | ||
traceback = traceback[:max_frames] + traceback[-max_frames:] # type: ignore |
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.
The slice is a Traceback
, but only the expression traceback[:max_frames] + traceback[-max_frames:]
is a List[…]
then.
No idea how to fix it, but it might help you.. :)
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.
Right, I misunderstood the situation. Looks like a deficiency in mypy/typeshed for now. I'll update the comment.
src/_pytest/_code/code.py
Outdated
reprfuncargs: Optional["ReprFuncArgs"], | ||
reprlocals: Optional["ReprLocals"], | ||
filelocrepr: Optional["ReprFileLocation"], | ||
style: 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.
Literal
for style
?
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.
Yes, will add.
@@ -976,6 +1035,7 @@ def __init__(self, lines, reprfuncargs, reprlocals, filelocrepr, style): | |||
|
|||
def toterminal(self, tw) -> None: | |||
if self.style == "short": | |||
assert self.reprfileloc is not None |
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.
Could (additionally) be checked/validated in __init__
? But it's fine like that also.
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.
Wouldn't happen unless the code is buggy; seems fine to only do it here.
@@ -1074,13 +1074,14 @@ def try_makedirs(cache_dir) -> bool: | |||
|
|||
def get_cache_dir(file_path: Path) -> Path: | |||
"""Returns the cache directory to write .pyc files for the given .py file path""" | |||
if sys.version_info >= (3, 8) and sys.pycache_prefix: | |||
# Type ignored until added in next mypy release. |
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.
Would be good to link an issue in general, but ok for now.
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 can detect outdated type: ignore
s so it's easy to get rid of when updating mypy.
warnings.warn(FUNCARGNAMES, stacklevel=2) | ||
return self.fixturenames | ||
|
||
|
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 ok with inlining, but wondered how it made typing harder (from the commit message)?
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.
It's a mixin class, it uses fixturenames
which is not defined in the class so mypy gets confused. There are ways to type this but it's not worth the bother in this case IMO.
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.
Agreed.
testing/code/test_source.py
Outdated
assert ex.value.lineno == 1 | ||
assert ex.value.offset in {5, 7} # cpython: 7, pypy3.6 7.1.1: 5 | ||
assert ex.value.text is not None | ||
assert ex.value.text.strip(), "x x" |
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.
Could check the full expected text instead?
assert ex.value.text == "xyz xyz\n"
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.
Yes, that's better.
IIRC this ( |
Yes, that's an option to. The |
51e3be2
to
eaa34a9
Compare
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 PR starts with some small prerequisite commits, and then adds type annotations to
_pytest.compat
and_pytest._code.code
.Let me know if this is too big for one PR, and I will split it. Note that some part of the diff is just adding
-> None
to some test functions, so that mypy will check them withoutcheck_untyped_defs = True
(which needs more work).