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

Add typing to from_parent return values #11916

Merged
merged 1 commit into from
Feb 23, 2024

Conversation

bluetech
Copy link
Member

@bluetech bluetech commented Feb 3, 2024

Up to now the return values of from_parent were untyped, this is an attempt to make it work with typing.Self.

Up to now the return values of `from_parent` were untyped, this is an
attempt to make it work with `typing.Self`.
Copy link
Member

@Zac-HD Zac-HD left a comment

Choose a reason for hiding this comment

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

Looks great - thanks @bluetech!

@Zac-HD Zac-HD merged commit 010ce2a into pytest-dev:main Feb 23, 2024
24 checks passed
flying-sheep pushed a commit to flying-sheep/pytest that referenced this pull request Apr 9, 2024
Up to now the return values of `from_parent` were untyped, this is an
attempt to make it work with `typing.Self`.
@Avasam
Copy link
Contributor

Avasam commented Aug 27, 2024

@bluetech This breaks the return type on Python 3.9 and under. You should be using from typing_extension import Self instead.

For example, the following code:

def pytest_collect_file(file_path: pathlib.Path, parent: Node) -> CheckdocsItem | None:
    if file_path.name not in project_files:
        return None
    return CheckdocsItem.from_parent(parent, name='project')

Will error with Returning Any from function declared to return "CheckdocsItem | None" [no-any-return] on 3.8 & 3.9, but pass on 3.10+ Which can only be worked around by disabling the error entirely in mypy, or doing:

def pytest_collect_file(file_path: pathlib.Path, parent: Node) -> CheckdocsItem | None:
    if file_path.name not in project_files:
        return None
    if sys.version_info >= (3, 10):  # pytest-dev/pytest#
        return CheckdocsItem.from_parent(parent, name='project')
    else:
        return cast(CheckdocsItem, CheckdocsItem.from_parent(parent, name='project'))

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