Skip to content

Generic type of setter is leaking across instances #8022

Open
@belm0

Description

@belm0

Bug description

Despite typing, the value setter below seems to remember the last assigned type, and applies it to an unrelated instance with different generic type.

from typing import TypeVar, Generic

T = TypeVar('T')


class WrappedVal(Generic[T]):
    def __init__(self, value: T):
        self._value = value

    @property
    def value(self) -> T:
        return self._value

    @value.setter
    def value(self, x: T):
        self._value = x


val1 = WrappedVal('hello')
val1.value = 'goodbye'

val2 = WrappedVal(1.)
print(-val2.value)  # bad operand type for unary -: str

Configuration

No response

Command used

pylint generic_setter.py

Pylint output

generic_setter.py:23:6: E1130: bad operand type for unary -: str (invalid-unary-operand-type)

Expected behavior

no error, since unary minus is valid for float

Pylint version

pylint 2.15.9
astroid 2.12.13
Python 3.8.12 (default, Sep 23 2021, 08:42:37)
[Clang 12.0.0 (clang-1200.0.32.29)]

OS / Environment

No response

Additional dependencies

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    False Positive 🦟A message is emitted but nothing is wrong with the codeNeeds PRThis issue is accepted, sufficiently specified and now needs an implementationtyping

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions