Skip to content

compare-to-empty-string false positive when the type is a union #8592

Closed as not planned
@dotkrnl

Description

@dotkrnl

Bug description

a: str | int = "" if some_condition else 0
if a == "":
    print("some_condition")

Configuration

load-plugins = "pylint.extensions.emptystring"

Command used

pylint

Pylint output

C1901: "a == ''" can be simplified to "not a" as an empty string is falsey (compare-to-empty-string)

Expected behavior

No output as a == '' can not be simplified to not a in this case. Note that a is int | str.

I know that using a datatype of int | str is a bad practice but apparently someone in our team decided to do this anyhow. In making the code compliant to pylint, this causes the behavior of the code to change.

I would suggest pylint to take type analysis into account when suggesting the falsey replacement, as can be simplified is misleading somehow. If this is not feasible, I feel that adding can be simplified if the type of "a" is "str" to the message, or adding a warning to the pylint.extensions.emptystring plugin would be helpful.

Pylint version

pylint 2.17.2
astroid 2.15.3
Python 3.10.10 (main, Feb  8 2023, 14:49:42) [GCC 7.5.0]

OS / Environment

No response

Additional dependencies

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions