Skip to content

Disable assignment-from-none for methods #5503

Open
@cdce8p

Description

@cdce8p
# pylint: disable=no-self-use

class Parent:
    def func(self) -> int | None:
        return None

    def serialize(self) -> str:
        x = self.func()  # assignment-from-none
        return f"Result: {x if x else ''}"

class Child(Parent):
    def func(self) -> int:
        return 42

In the example above, assignment-from-none is emitted on a method call even though func() could be overwritten and not return None.

Proposal
Limit the assignment-from-none check to functions. Methods should only be considered if marked with @typing.final. It might also work to check the return type itself to see if it's only -> None.

Reason
We can't reliably determine if a method is ever overwritten and thus return something else then None.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions