Skip to content

Avoid redundant extra errors after "Need type annotation for variable" #6998

Closed
@ilevkivskyi

Description

@ilevkivskyi

Consider this (oversimplified) example:

from typing import TypeVar, Optional

T = TypeVar('T')

def f(x: Optional[T] = None) -> T: ...

x = f()  # Need type annotation for 'x'
y = x  # Cannot determine type of 'x'

def g() -> None:
    x = f()  # Need type annotation for 'x'
    y = x  # Cannot determine type of 'x'

The subsequent errors look redundant (and maybe even annoying). This is more important with the new semantic analyzer because the old one doesn't show the error if the problematic variable was defined at the function scope.

Potentially, we can replace <nothing> with Any after we show the error. Also the code that detects failed inference can be improved. For example, is_valid_inferred_type() should be replaced with a proper type visitor.

Metadata

Metadata

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions