Skip to content

typing: pytest_collection #6601

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 1 commit into from
Jan 30, 2020

Conversation

blueyed
Copy link
Contributor

@blueyed blueyed commented Jan 28, 2020

Not included in #6556, but conflicts (trivially).

@@ -158,7 +166,7 @@ def pytest_load_initial_conftests(early_config, parser, args):


@hookspec(firstresult=True)
def pytest_collection(session):
def pytest_collection(session: "Session") -> Optional[Any]:
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Optional[Any]: the return values are not used, this is meant to indicate that.

@blueyed blueyed force-pushed the typing-pytest_collection branch from c6e612f to 740a70c Compare January 28, 2020 16:18
@blueyed blueyed requested a review from bluetech January 28, 2020 23:26
def _main(
config: Config, session: "Session"
) -> Optional[
Union["Literal"[ExitCode.TESTS_FAILED], "Literal"[ExitCode.NO_TESTS_COLLECTED]]
Copy link
Member

Choose a reason for hiding this comment

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

Are you sure _main will only ever return these two error codes? If it is put in the type annotation, then once they're published, it becomes an API guarantee.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's what is used in that function. Also _main is not really public API, is it?

Copy link
Member

Choose a reason for hiding this comment

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

It's what used in the function but it's better to keep our options open IMO.

IIUC, eventually this return value is funneled to the main function in _pytest/config/__init__.py which has return type Union[int, _pytest.main.ExitCode]. My suggestion is to use the same type here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry, I fail to see why. If a function returns only 1, or 2, it should/could have Literal[1, 2], even when used from another function that returns int, no?

Copy link
Member

Choose a reason for hiding this comment

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

This is really not very important but I'll try to explain my view.

I think restricting the type in such a way is not beneficial. The function is an (indirect) implementation of a hook and the only restriction that needs to apply to it is that it implements the hook specification.

Unless this is an actual API guarantee that should not be changed without it being a breaking change, the fact that it currently returns these two values is just an incidental implementation detail. If someone wants to say add a new error code, then they 1) might be unsure if they're allowed to change the type, and 2) need to "uselessly" adjust the type.

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've really seen it more like being able to see from the signature already, what it might "raise".
But I do not have a strong opinion on it, and certainly trust you more with regard to type hinting.. :)
Amended.

Copy link
Member

Choose a reason for hiding this comment

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

Thanks for indulging me :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@bluetech just for understanding: would you type a function always returning True also with bool then? (https://github.com/blueyed/pytest/blob/70481429f8236141e3a39098082b254f4a1cc017/src/_pytest/python.py#L172)

My general understanding/idea with that was that we could later tell that a certain hook, when used, would always skip later ones. This is somehow indicated with -> bool also (not Optional[bool], but following your argument it should be typed like the hookspec, which is Optional[…]/Optional[Any] then for firstresult=True.

Copy link
Member

Choose a reason for hiding this comment

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

I'd tend to use the same type as the hookspec. I see that fact that the impl currently skips (returns True) is a runtime property, which might change. But if it's really fundamental, as in we want to "enforce" that this particular impl always skips, then making it -> True does make sense.

@blueyed blueyed force-pushed the typing-pytest_collection branch 3 times, most recently from b4922c9 to f587b28 Compare January 30, 2020 18:51
@blueyed blueyed force-pushed the typing-pytest_collection branch from f587b28 to 88b8003 Compare January 30, 2020 18:58
@blueyed blueyed merged commit e830432 into pytest-dev:master Jan 30, 2020
@blueyed blueyed deleted the typing-pytest_collection branch January 30, 2020 19:47
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