Skip to content

typing: Session.__init__ #6525

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 22, 2020
Merged

typing: Session.__init__ #6525

merged 1 commit into from
Jan 22, 2020

Conversation

blueyed
Copy link
Contributor

@blueyed blueyed commented Jan 21, 2020

Pulled out of #6491.

@blueyed blueyed mentioned this pull request Jan 21, 2020
2 tasks
@blueyed blueyed requested a review from nicoddemus January 22, 2020 18:26
@@ -383,7 +391,7 @@ class Session(nodes.FSCollector):
# Set on the session by fixtures.pytest_sessionstart.
_fixturemanager = None # type: FixtureManager

def __init__(self, config):
def __init__(self, config) -> None:
Copy link
Member

Choose a reason for hiding this comment

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

Out of curiosity, is considered good practice to annotate the return value of all functions even if they return 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.

It has to be for the function to be considered to be typed.
I've amended it to also type config (the arg) explicitly, although done via base class.

Copy link
Member

@nicoddemus nicoddemus Jan 22, 2020

Choose a reason for hiding this comment

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

I see, thanks makes sense, it is the only annotation.

Now it brings the next question, is:

def __init__(self, config: Config):

enough for it to be considered typed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes (AFAIK), but might fail with strict options etc?! /cc @bluetech

Copy link
Contributor Author

Choose a reason for hiding this comment

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

btw: reveal_type(var) is very useful in that regard.

@blueyed blueyed merged commit ad02f6f into pytest-dev:master Jan 22, 2020
@blueyed blueyed deleted the typing-session branch January 22, 2020 23:16
# Keep track of any collected nodes in here, so we don't duplicate fixtures
self._node_cache = {}
self._node_cache = {} # type: Dict[str, List[Node]]
Copy link
Member

Choose a reason for hiding this comment

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

Apparently sometimes the key can be tuple so the type is not exactly correct:

pytest/src/_pytest/main.py

Lines 574 to 579 in ad02f6f

key = (type(x), x.fspath)
if key in self._node_cache:
yield self._node_cache[key]
else:
self._node_cache[key] = x
yield x

I am working on some annotations and will include a fix for this, hopefully tomorrow.

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.

3 participants