Skip to content

Narrowing to a protocol loses information about self type #6434

Closed

Description

import dataclasses
from dataclasses import dataclass, replace
from typing import Any, ClassVar, Generic, Protocol, Self, TypeVar


class DataclassInstance(Protocol):
    __dataclass_fields__: ClassVar[dict[str, dataclasses.Field[Any]]]


T = TypeVar('T')
@dataclass
class X(Generic[T]):
    a: int

    def f(self) -> Self:
        reveal_type(self)  # Self@X[T@X]
        assert isinstance(self, DataclassInstance)
        reveal_type(self)  # X[T@X]
        return replace(self, a=1)  # Error, can't return X.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    addressed in next versionIssue is fixed and will appear in next published versionbugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions