Skip to content

Wrong mypy behavior with warn_unreachable flag #18785

Closed as not planned
Closed as not planned
@Sanchoyzer

Description

@Sanchoyzer
mypy==1.15.0

[tool.mypy]
python_version = '3.12'
show_error_context = true
check_untyped_defs = true
ignore_missing_imports = true
warn_unused_ignores = true
warn_redundant_casts = true
show_error_code_links = true
warn_unreachable = true

Gist URL: https://gist.github.com/mypy-play/187d99115a24b12a7a58b437ae6127fb

Playground URL: https://mypy-play.net/?mypy=1.15.0&python=3.12&flags=ignore-missing-imports%2Ccheck-untyped-defs%2Cwarn-unreachable&gist=187d99115a24b12a7a58b437ae6127fb

For some reason, mypy displays the error "Statement is unreachable [unreachable]" for actual code that executes.

async_session: async_sessionmaker = async_sessionmaker(...)


class ProjectStatus(StrEnum):
    NEW = 'new'
    PENDING = 'pending'
    FINISHED = 'finished'


class Project(Base):
    __tablename__ = 'projects'

    uid: Mapped[int] = mapped_column(primary_key=True)
    status: Mapped[ProjectStatus] = mapped_column(
        Enum(*list(ProjectStatus._value2member_map_.keys()), name='project_status_enum'),
        server_default=ProjectStatus.NEW,
    )


@pytest_asyncio.fixture
async def project_id() -> int:
    ...


async def update_status(project_id: int, status: ProjectStatus) -> None:
    ...


async def test_project_status(project_id: int) -> None:
    async with async_session() as session:
        tmp: Project | None = await session.get(Project, project_id)
        assert tmp
        project: Project = tmp
        assert project.status == ProjectStatus.NEW  # if comment this line, the "unreachable" error will disappear

        for status in [ProjectStatus.PENDING, ProjectStatus.FINISHED]:
            await update_status(project_id=project.uid, status=status)
            await session.refresh(project)
            assert project.status == status
            if status == ProjectStatus.FINISHED:
                print('the end')  # error: Statement is unreachable  [unreachable]

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongtopic-reachabilityDetecting unreachable code

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions