pylint-dev / pylint

It's not just a linter that annoys you!
https://pylint.readthedocs.io/en/latest/
GNU General Public License v2.0
5.32k stars 1.14k forks source link

Pylint 3.3.0 does not recognize f-string as string and reports no-member for startswith() #9947

Closed andy-maier closed 1 month ago

andy-maier commented 1 month ago

Bug description

The following file test_pylint.py:

#!/usr/bin/env python
"""module"""

class Cls:
    # pylint: disable=too-few-public-methods
    """Cls"""

c_obj = Cls()

c_repr1 = '{!r}'.format(c_obj)  # pylint: disable=consider-using-f-string
c_repr2 = f'{c_obj!r}'

assert c_repr1.startswith('<__main__.Cls')
assert c_repr2.startswith('<__main__.Cls')  # pylint reports no-member

causes pylint to report this issue:

$ pylint test_pylint.py 
************* Module tmp_issues.test_pylint
test_pylint.py:16:7: E1101: Instance of 'Cls' has no 'startswith' member (no-member)

I tested this with multiple pylint versions, and the first version that has this behavior is pylint 3.3.0.

Command used

pylint test_pylint.py

Pylint output

************* Module test_pylint
test_pylint.py:16:7: E1101: Instance of 'Cls' has no 'startswith' member (no-member)

Expected behavior

Pylint should not report this as an error, because an f-string is a string and has the startswith() method.

Pylint version

pylint    3.3.0
astroid   3.3.3
Python 3.12.4 (main, Jun  6 2024, 18:26:44) [Clang 15.0.0 (clang-1500.3.9.4)]

OS / Environment

macOS Sonoma 14.6.1

jacobtylerwalls commented 1 month ago

Thanks for the report, regression in pylint-dev/astroid@3dbf1393de191fd10b09741badfe1b075fdadbac

jacobtylerwalls commented 1 month ago

cc/ @ericvergnaud, would you by chance be able to have a look? 🙏

ericvergnaud commented 1 month ago

cc/ @ericvergnaud, would you by chance be able to have a look? 🙏

Will do tomorrow

oprypin commented 1 month ago

I think I encountered a regression with the same root cause:

Regressed with astroid>=3.3:

"""Hi."""

class Foo:
    """Hi."""

    def hi(self):
        """Hi."""

    def __repr__(self):  # E0306: __repr__ does not return str (invalid-repr-returned)
        return f'{5}'
ericvergnaud commented 1 month ago

@jacobtylerwalls see https://github.com/pylint-dev/astroid/pull/2578