Skip to content

Commit d4a9671

Browse files
Merge branch 'master' into str-format-folding
2 parents 2665c94 + 1b8841b commit d4a9671

File tree

12 files changed

+53
-102
lines changed

12 files changed

+53
-102
lines changed

mypy/cache.py

Lines changed: 14 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,22 @@
11
from __future__ import annotations
22

33
from collections.abc import Sequence
4-
from typing import TYPE_CHECKING, Final
4+
from typing import Final
55

66
from mypy_extensions import u8
7-
8-
try:
9-
from native_internal import (
10-
Buffer as Buffer,
11-
read_bool as read_bool,
12-
read_float as read_float,
13-
read_int as read_int,
14-
read_str as read_str,
15-
read_tag as read_tag,
16-
write_bool as write_bool,
17-
write_float as write_float,
18-
write_int as write_int,
19-
write_str as write_str,
20-
write_tag as write_tag,
21-
)
22-
except ImportError:
23-
# TODO: temporary, remove this after we publish mypy-native on PyPI.
24-
if not TYPE_CHECKING:
25-
26-
class Buffer:
27-
def __init__(self, source: bytes = b"") -> None:
28-
raise NotImplementedError
29-
30-
def getvalue(self) -> bytes:
31-
raise NotImplementedError
32-
33-
def read_int(data: Buffer) -> int:
34-
raise NotImplementedError
35-
36-
def write_int(data: Buffer, value: int) -> None:
37-
raise NotImplementedError
38-
39-
def read_tag(data: Buffer) -> u8:
40-
raise NotImplementedError
41-
42-
def write_tag(data: Buffer, value: u8) -> None:
43-
raise NotImplementedError
44-
45-
def read_str(data: Buffer) -> str:
46-
raise NotImplementedError
47-
48-
def write_str(data: Buffer, value: str) -> None:
49-
raise NotImplementedError
50-
51-
def read_bool(data: Buffer) -> bool:
52-
raise NotImplementedError
53-
54-
def write_bool(data: Buffer, value: bool) -> None:
55-
raise NotImplementedError
56-
57-
def read_float(data: Buffer) -> float:
58-
raise NotImplementedError
59-
60-
def write_float(data: Buffer, value: float) -> None:
61-
raise NotImplementedError
62-
7+
from native_internal import (
8+
Buffer as Buffer,
9+
read_bool as read_bool,
10+
read_float as read_float,
11+
read_int as read_int,
12+
read_str as read_str,
13+
read_tag as read_tag,
14+
write_bool as write_bool,
15+
write_float as write_float,
16+
write_int as write_int,
17+
write_str as write_str,
18+
write_tag as write_tag,
19+
)
6320

6421
# Always use this type alias to refer to type tags.
6522
Tag = u8

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 (
@@ -27,9 +27,11 @@
2727
UnaryExpr,
2828
Var,
2929
)
30-
from mypyc.irbuild.builder import IRBuilder
3130
from mypyc.irbuild.util import bytes_from_str
3231

32+
if TYPE_CHECKING:
33+
from mypyc.irbuild.builder import IRBuilder
34+
3335
# All possible result types of constant folding
3436
ConstantValue = Union[int, float, complex, str, bytes]
3537
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))

0 commit comments

Comments
 (0)