Skip to content

Commit bd25428

Browse files
Merge branch 'master' into patch-11
2 parents 651e5f3 + 1b8841b commit bd25428

File tree

11 files changed

+39
-45
lines changed

11 files changed

+39
-45
lines changed

mypy/checkexpr.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from mypy.checker_shared import ExpressionCheckerSharedApi
1919
from mypy.checkmember import analyze_member_access, has_operator
2020
from mypy.checkstrformat import StringFormatterChecker
21+
from mypy.constant_fold import constant_fold_expr
2122
from mypy.erasetype import erase_type, remove_instance_last_known_values, replace_meta_vars
2223
from mypy.errors import ErrorInfo, ErrorWatcher, report_internal_error
2324
from mypy.expandtype import (
@@ -656,11 +657,12 @@ def visit_call_expr_inner(self, e: CallExpr, allow_none_return: bool = False) ->
656657
return ret_type
657658

658659
def check_str_format_call(self, e: CallExpr) -> None:
659-
"""More precise type checking for str.format() calls on literals."""
660+
"""More precise type checking for str.format() calls on literals and folded constants."""
660661
assert isinstance(e.callee, MemberExpr)
661662
format_value = None
662-
if isinstance(e.callee.expr, StrExpr):
663-
format_value = e.callee.expr.value
663+
folded_callee_expr = constant_fold_expr(e.callee.expr, "<unused>")
664+
if isinstance(folded_callee_expr, str):
665+
format_value = folded_callee_expr
664666
elif self.chk.has_type(e.callee.expr):
665667
typ = get_proper_type(self.chk.lookup_type(e.callee.expr))
666668
if (

mypy/main.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1066,11 +1066,7 @@ def add_invertible_flag(
10661066
incremental_group.add_argument(
10671067
"--fixed-format-cache",
10681068
action="store_true",
1069-
help=(
1070-
"Use experimental fast and compact fixed format cache"
1071-
if compilation_status == "yes"
1072-
else argparse.SUPPRESS
1073-
),
1069+
help="Use new fast and compact fixed format cache",
10741070
)
10751071
incremental_group.add_argument(
10761072
"--skip-version-check",

mypy/modulefinder.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -796,7 +796,7 @@ def default_lib_path(
796796
custom_typeshed_dir = os.path.abspath(custom_typeshed_dir)
797797
typeshed_dir = os.path.join(custom_typeshed_dir, "stdlib")
798798
mypy_extensions_dir = os.path.join(custom_typeshed_dir, "stubs", "mypy-extensions")
799-
mypy_native_dir = os.path.join(custom_typeshed_dir, "stubs", "mypy-native")
799+
librt_dir = os.path.join(custom_typeshed_dir, "stubs", "librt")
800800
versions_file = os.path.join(typeshed_dir, "VERSIONS")
801801
if not os.path.isdir(typeshed_dir) or not os.path.isfile(versions_file):
802802
print(
@@ -812,13 +812,13 @@ def default_lib_path(
812812
data_dir = auto
813813
typeshed_dir = os.path.join(data_dir, "typeshed", "stdlib")
814814
mypy_extensions_dir = os.path.join(data_dir, "typeshed", "stubs", "mypy-extensions")
815-
mypy_native_dir = os.path.join(data_dir, "typeshed", "stubs", "mypy-native")
815+
librt_dir = os.path.join(data_dir, "typeshed", "stubs", "librt")
816816
path.append(typeshed_dir)
817817

818-
# Get mypy-extensions and mypy-native stubs from typeshed, since we treat them as
818+
# Get mypy-extensions and librt stubs from typeshed, since we treat them as
819819
# "internal" libraries, similar to typing and typing-extensions.
820820
path.append(mypy_extensions_dir)
821-
path.append(mypy_native_dir)
821+
path.append(librt_dir)
822822

823823
# Add fallback path that can be used if we have a broken installation.
824824
if sys.platform != "win32":
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
version = "0.1.*"

mypy/typeshed/stubs/mypy-native/METADATA.toml

Lines changed: 0 additions & 1 deletion
This file was deleted.

mypyc/irbuild/builder.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
TypeAlias,
4141
TypeInfo,
4242
TypeParam,
43-
UnaryExpr,
4443
Var,
4544
)
4645
from mypy.types import (
@@ -106,6 +105,7 @@
106105
object_rprimitive,
107106
str_rprimitive,
108107
)
108+
from mypyc.irbuild.constant_fold import constant_fold_expr
109109
from mypyc.irbuild.context import FuncInfo, ImplicitClass
110110
from mypyc.irbuild.ll_builder import LowLevelIRBuilder
111111
from mypyc.irbuild.mapper import Mapper
@@ -965,12 +965,8 @@ def maybe_spill_assignable(self, value: Value) -> Register | AssignmentTarget:
965965
return reg
966966

967967
def extract_int(self, e: Expression) -> int | None:
968-
if isinstance(e, IntExpr):
969-
return e.value
970-
elif isinstance(e, UnaryExpr) and e.op == "-" and isinstance(e.expr, IntExpr):
971-
return -e.expr.value
972-
else:
973-
return None
968+
folded = constant_fold_expr(self, e)
969+
return folded if isinstance(folded, int) else None
974970

975971
def get_sequence_type(self, expr: Expression) -> RType:
976972
return self.get_sequence_type_from_type(self.types[expr])

mypyc/irbuild/constant_fold.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
from __future__ import annotations
1212

13-
from typing import Final, Union
13+
from typing import TYPE_CHECKING, Final, Union
1414

1515
from mypy.constant_fold import constant_fold_binary_op, constant_fold_unary_op
1616
from mypy.nodes import (
@@ -26,9 +26,11 @@
2626
UnaryExpr,
2727
Var,
2828
)
29-
from mypyc.irbuild.builder import IRBuilder
3029
from mypyc.irbuild.util import bytes_from_str
3130

31+
if TYPE_CHECKING:
32+
from mypyc.irbuild.builder import IRBuilder
33+
3234
# All possible result types of constant folding
3335
ConstantValue = Union[int, float, complex, str, bytes]
3436
CONST_TYPES: Final = (int, float, complex, str, bytes)

mypyc/irbuild/expression.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,7 @@ def transform_op_expr(builder: IRBuilder, expr: OpExpr) -> Value:
558558
# Special case some int ops to allow borrowing operands.
559559
if is_int_rprimitive(ltype) and is_int_rprimitive(rtype):
560560
if expr.op == "//":
561-
expr = try_optimize_int_floor_divide(expr)
561+
expr = try_optimize_int_floor_divide(builder, expr)
562562
if expr.op in int_borrow_friendly_op:
563563
borrow_left = is_borrow_friendly_expr(builder, expr.right)
564564
borrow_right = True
@@ -571,11 +571,11 @@ def transform_op_expr(builder: IRBuilder, expr: OpExpr) -> Value:
571571
return builder.binary_op(left, right, expr.op, expr.line)
572572

573573

574-
def try_optimize_int_floor_divide(expr: OpExpr) -> OpExpr:
574+
def try_optimize_int_floor_divide(builder: IRBuilder, expr: OpExpr) -> OpExpr:
575575
"""Replace // with a power of two with a right shift, if possible."""
576-
if not isinstance(expr.right, IntExpr):
576+
divisor = constant_fold_expr(builder, expr.right)
577+
if not isinstance(divisor, int):
577578
return expr
578-
divisor = expr.right.value
579579
shift = divisor.bit_length() - 1
580580
if 0 < shift < 28 and divisor == (1 << shift):
581581
return OpExpr(">>", expr.left, IntExpr(shift))

mypyc/irbuild/specialize.py

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
from mypy.nodes import (
2020
ARG_NAMED,
2121
ARG_POS,
22-
BytesExpr,
2322
CallExpr,
2423
DictExpr,
2524
Expression,
@@ -78,6 +77,7 @@
7877
uint8_rprimitive,
7978
)
8079
from mypyc.irbuild.builder import IRBuilder
80+
from mypyc.irbuild.constant_fold import constant_fold_expr
8181
from mypyc.irbuild.for_helpers import (
8282
comprehension_helper,
8383
sequence_from_generator_preallocate_helper,
@@ -716,21 +716,18 @@ def translate_dict_setdefault(builder: IRBuilder, expr: CallExpr, callee: RefExp
716716

717717
@specialize_function("format", str_rprimitive)
718718
def translate_str_format(builder: IRBuilder, expr: CallExpr, callee: RefExpr) -> Value | None:
719-
if (
720-
isinstance(callee, MemberExpr)
721-
and isinstance(callee.expr, StrExpr)
722-
and expr.arg_kinds.count(ARG_POS) == len(expr.arg_kinds)
723-
):
724-
format_str = callee.expr.value
725-
tokens = tokenizer_format_call(format_str)
726-
if tokens is None:
727-
return None
728-
literals, format_ops = tokens
729-
# Convert variables to strings
730-
substitutions = convert_format_expr_to_str(builder, format_ops, expr.args, expr.line)
731-
if substitutions is None:
732-
return None
733-
return join_formatted_strings(builder, literals, substitutions, expr.line)
719+
if isinstance(callee, MemberExpr):
720+
folded_callee = constant_fold_expr(builder, callee.expr)
721+
if isinstance(folded_callee, str) and expr.arg_kinds.count(ARG_POS) == len(expr.arg_kinds):
722+
tokens = tokenizer_format_call(folded_callee)
723+
if tokens is None:
724+
return None
725+
literals, format_ops = tokens
726+
# Convert variables to strings
727+
substitutions = convert_format_expr_to_str(builder, format_ops, expr.args, expr.line)
728+
if substitutions is None:
729+
return None
730+
return join_formatted_strings(builder, literals, substitutions, expr.line)
734731
return None
735732

736733

@@ -1059,9 +1056,9 @@ def translate_float(builder: IRBuilder, expr: CallExpr, callee: RefExpr) -> Valu
10591056
def translate_ord(builder: IRBuilder, expr: CallExpr, callee: RefExpr) -> Value | None:
10601057
if len(expr.args) != 1 or expr.arg_kinds[0] != ARG_POS:
10611058
return None
1062-
arg = expr.args[0]
1063-
if isinstance(arg, (StrExpr, BytesExpr)) and len(arg.value) == 1:
1064-
return Integer(ord(arg.value))
1059+
arg = constant_fold_expr(builder, expr.args[0])
1060+
if isinstance(arg, (str, bytes)) and len(arg) == 1:
1061+
return Integer(ord(arg))
10651062
return None
10661063

10671064

0 commit comments

Comments
 (0)