Skip to content
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

[RFC] typing around terminal #6157

Merged
merged 1 commit into from
Nov 9, 2019
Merged

Conversation

blueyed
Copy link
Contributor

@blueyed blueyed commented Nov 8, 2019

No description provided.

self._showfspath = None

self.stats = {}
# XXX: needs TypedDict? warnings => WarningReport, others => BaseReport
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can do it, only issue is that unless we want to bring in a runtime dependency on typing_extension, we'll need to add a compat for it, or always surround it with if TYPE_CHECKING and string annotations.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is rather out of scope, since it requires py38 (IIRC).
Better options?
Otherwise I will remove the XXX/TODO comments about it.

We could also have stats_warnings explicitly for this, but that's not B/C (but could be done via a property or thelike then).
But IIRC I've tried a property based approach (where I would define the returned type), which did not help really.

@blueyed blueyed changed the title [WIP] typing around terminal [RFC] typing around terminal Nov 9, 2019
@blueyed
Copy link
Contributor Author

blueyed commented Nov 9, 2019

@bluetech any input here, please?

Copy link
Member

@bluetech bluetech left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, left some mostly optional comments.

@@ -223,7 +223,7 @@ def __init__(
#: a (filesystempath, lineno, domaininfo) tuple indicating the
#: actual location of a test item - it might be different from the
#: collected one e.g. if a method is inherited from a different module.
self.location = location
self.location = location # type: Tuple[str, Optional[int], 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 think that adding an annotation to a function whose own signature is unannotated doesn't have an effect (ref).

So it is better to add this annotation to the location parameter instead, if possible. This will make mypy consider the function to be typed.

Copy link
Contributor Author

@blueyed blueyed Nov 9, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reveal_type gives me 'Tuple[builtins.str, Union[builtins.int, None], builtins.str]' here (mypy 0.750+dev.84126ab9985844d8cda3809688034af895fdaf7c.dirty).

This appears to come / being enabled via BaseReport - adding it there (and to the def here) instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But it is needed to fix "List or tuple expected as variable arguments" with https://github.com/blueyed/pytest/blob/0133097943e0140ab82e07d4d201405145d35e59/src/_pytest/terminal.py#L442.
But it seems strange that with "location: Tuple[str, Optional[int], str]" in TestReport.__init__ and rep being _pytest.reports.TestReport, rep.location is Union[Tuple[builtins.str, Union[builtins.int, None], builtins.str], None] then?

The problem appears to be that BaseReport has location = None # type: Optional[Tuple[str, Optional[int], str]], and we cannot have location: Tuple[str, Optional[int], str] in TestReport (py35).
Adding an assert appears to work also, but then again it is better to keep the type comment there.

src/_pytest/terminal.py Outdated Show resolved Hide resolved
src/_pytest/terminal.py Outdated Show resolved Hide resolved
@@ -233,29 +238,30 @@ def get_location(self, config):


class TerminalReporter:
def __init__(self, config, file=None):
def __init__(self, config: Config, file=None):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-> None

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a way to have mypy complain about it?

@@ -440,7 +446,7 @@ def pytest_runtest_logreport(self, report):
self._write_progress_information_filling_space()
else:
self.ensure_newline()
self._tw.write("[%s]" % rep.node.gateway.id)
self._tw.write("[%s]" % rep.node.gateway.id) # type: ignore
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be nice to have a short comment on why the type: ignore is needed here. This why we'll be able to tell later whether it's a "TODO" or a "workaround mypy" thing.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a workaround-xdist thing (I assume that xdist just puts it there).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll keep it as is (it is somehow obvious from "xdist" in there, and I do not like extra comments for this above). Also it might get reported when not being used/needed later anymore (there's a mode in mypy to do so).

self._showfspath = None

self.stats = {}
# XXX: needs TypedDict? warnings => WarningReport, others => BaseReport
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can do it, only issue is that unless we want to bring in a runtime dependency on typing_extension, we'll need to add a compat for it, or always surround it with if TYPE_CHECKING and string annotations.

@blueyed blueyed merged commit 710e3c4 into pytest-dev:features Nov 9, 2019
@blueyed blueyed deleted the typing-terminal branch November 9, 2019 23:19
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