8000 --warn-return-any --redundant-cast inconsistencey · Issue #4602 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content
--warn-return-any --redundant-cast inconsistencey #4602
Closed
@kamahen

Description

@kamahen

mypy --python-version=3.6 --strict-optional --check-untyped-defs --disallow-incomplete-defs --warn-incomplete-stub --warn-redundant-casts --warn-no-return --warn-return-any --no-incremental

This (simplified) code generates a warning: Returning Any from function declared to return "_SelfType" (for context, this code implements a base class similar to namedtuple but using regular classes instead of tuple):

class PlainOldData:

    __slots__ = ()  # type: Sequence[str]

       _SelfType = TypeVar('_SelfType', bound='PlainOldData')

    def _replace(self: _SelfType, **kwargs: Dict[Text, Any]) -> _SelfType:  # pylint: disable=undefined-variable
        """Make a new object, replacing fields with new values."""
        new_attrs = {
            k: kwargs.pop(k) if k in kwargs else getattr(self, k)
            for k in self.__slots__
        }
        if kwargs:
            raise ValueError('Unknown field names: {!r}'.format(list(kwargs)))
        return self.__class__(**new_attrs)

When I change the return statement to return cast(self._SelfType, self.__class__(**new_attrs)), I get Redundant cast to self._SelfType?

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions

    0