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

TestReport.when/location have the wrong type reported by type checkers #12941

Open
asqui opened this issue Nov 5, 2024 · 0 comments
Open

TestReport.when/location have the wrong type reported by type checkers #12941

asqui opened this issue Nov 5, 2024 · 0 comments

Comments

@asqui
Copy link

asqui commented Nov 5, 2024

Type checkers report the type of TestReport.when as Union[builtins.str, None] (coming from the BaseReport base class) rather than the correct type for TestReport of Literal["setup", "call", "teardown"].

(I checked this with mypy 1.10.0 and pyright 1.1.342)

This looks to be due to the fact that the BaseReport annotation is on a class attribute, which takes precedence over the instance attribute assigned within the derived TestReport class.

Repro

def pytest_runtest_logreport(report: TestReport) -> None:
    reveal_type(report.when)

mypy 1.10.0 reports:

note: Revealed type is "Union[builtins.str, None]"

(The actual type is Literal["setup", "call", "teardown"])

Suggested Solution

I think this can be resolved by adding a class-level type annotation to TestReport with the narrower type (the same applies to location):

@final
class TestReport(BaseReport):
    # [...]
    when: Literal["setup", "call", "teardown"]
    location: tuple[str, int | None, str]
    # [...]
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

No branches or pull requests

1 participant