Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -6543,10 +6543,6 @@ def comparison_type_narrowing_helper(self, node: ComparisonExpr) -> tuple[TypeMa

if (
literal(expr) == LITERAL_TYPE
and not is_literal_none(expr)
and not is_literal_not_implemented(expr)
and not is_false_literal(expr)
and not is_true_literal(expr)
and not self.is_literal_enum(expr)
and not (
isinstance(p_expr := get_proper_type(expr_type), CallableType)
Expand Down
23 changes: 20 additions & 3 deletions mypy/literals.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
OpExpr,
ParamSpecExpr,
PromoteExpr,
RefExpr,
RevealExpr,
SetComprehension,
SetExpr,
Expand All @@ -44,6 +45,7 @@
SuperExpr,
TempNode,
TupleExpr,
TypeAlias,
TypeAliasExpr,
TypeApplication,
TypedDictExpr,
Expand All @@ -55,6 +57,7 @@
YieldExpr,
YieldFromExpr,
)
from mypy.types import is_named_instance
from mypy.visitor import ExpressionVisitor

# [Note Literals and literal_hash]
Expand Down Expand Up @@ -135,10 +138,24 @@ def literal(e: Expression) -> int:
else:
return LITERAL_NO

elif isinstance(e, NameExpr):
if isinstance(e.node, Var) and e.node.is_final and e.node.final_value is not None:
elif isinstance(e, RefExpr):
if (
isinstance(e, NameExpr)
and isinstance(e.node, Var)
and e.node.is_final
and e.node.final_value is not None
):
return LITERAL_YES

NAMES = ("builtins.True", "builtins.False", "builtins.None", "builtins.NotImplemented")
if e.fullname in NAMES:
return LITERAL_YES
return LITERAL_TYPE
elif isinstance(e.node, TypeAlias) and not e.node.python_3_12_type_alias:
if is_named_instance(e.node.target, NAMES):
return LITERAL_YES

if isinstance(e, NameExpr):
return LITERAL_TYPE

if isinstance(e, (IntExpr, FloatExpr, ComplexExpr, StrExpr, BytesExpr)):
return LITERAL_YES
Expand Down