Skip to content

[unsubscriptable-object] False Positive on Optional list #9515

@embonhomme

Description

@embonhomme

Bug description

# Optional method arguments or local scope variables are not properly inferred to be list[str] when in the if body. # example.py from typing import Optional from pydantic import BaseModel class Parents(BaseModel): name: str age: int children_name: Optional[list[str]] = None def get_first_children_name(self) -> Optional[str]: if self.children_name: return self.children_name[0] return None

Configuration

[FORMAT] # Maximum number of characters on a single line. max-line-length = 120 [MESSAGE CONTROL] disable = missing-module-docstring, missing-function-docstring, missing-class-docstring, fixme, unused-argument, broad-exception-caught, too-few-public-methods, import-error, no-name-in-module [BASIC] good-names= i, e, logger [MASTER] # disable false positives for Pydantic, which uses C extensions # see https://github.com/pydantic/pydantic/issues/1961#issuecomment-759522422 extension-pkg-whitelist = pydantic

Command used

pylint --reports=n example.py

Pylint output

E1136: Value 'self.children_name' is unsubscriptable (unsubscriptable-object)

Expected behavior

No error at all

Pylint version

pylint 2.17.5 astroid 2.15.6 Python 3.11.6 (main, Nov 6 2023, 08:36:52) [GCC 9.4.0]

OS / Environment

No response

Additional dependencies

pydantic-core 2.6.3 typing-extensions 4.7.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    AstroidRelated to astroidC: unsubscriptable-objectIssues related to 'unsubscriptable-object' checkFalse Positive 🦟A message is emitted but nothing is wrong with the codeNeeds PRThis issue is accepted, sufficiently specified and now needs an implementation

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions