Skip to content

After Optional[str] == 'literal', infer that it's not optional #1825

Closed
@gvanrossum

Description

@gvanrossum

Example:

def f(a: Optional[str]) -> str:
    if a == '<string>': return a[1:-1]
    else: return ''

With --strict-optional, this produces an error:

error: Value of type None is not indexable

It would be nice if mypy understood that a is a string if this == returns True.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions