🌐 AI搜索 & 代理 主页
Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
2b458bc
feat: dict literals wip
BobTheBuidler Sep 17, 2025
ab47db9
finish dict literals
BobTheBuidler Sep 17, 2025
b03b6b6
fix: nested
BobTheBuidler Sep 17, 2025
03e2ccb
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 17, 2025
6abe3e5
fix mypy errs
BobTheBuidler Sep 17, 2025
fd075dd
Merge branch 'dict-literals' of https://github.com/BobTheBuidler/mypy…
BobTheBuidler Sep 17, 2025
7fa3a98
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 17, 2025
f298714
fix: no nested
BobTheBuidler Sep 17, 2025
e08fa78
Merge branch 'dict-literals' of https://github.com/BobTheBuidler/mypy…
BobTheBuidler Sep 17, 2025
f754cc0
fix ir
BobTheBuidler Sep 17, 2025
691c64e
Update run-dicts.test
BobTheBuidler Sep 17, 2025
5d15490
Update emitmodule.py
BobTheBuidler Sep 17, 2025
45578b6
Update ircheck.py
BobTheBuidler Sep 17, 2025
23d6cb5
Update ircheck.py
BobTheBuidler Sep 17, 2025
7a1d7df
Update expression.py
BobTheBuidler Sep 17, 2025
6597401
)
BobTheBuidler Sep 17, 2025
3a49462
fix ir
BobTheBuidler Sep 17, 2025
7f8bb37
fix mypy errs
BobTheBuidler Sep 17, 2025
0d7b234
fix mypy errs
BobTheBuidler Sep 17, 2025
7cd9c62
fix mypy errs
BobTheBuidler Sep 17, 2025
fad0915
handle tuples
BobTheBuidler Sep 17, 2025
bd42865
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 17, 2025
6bbb9cf
fix name err
BobTheBuidler Sep 17, 2025
68b3633
Merge branch 'dict-literals' of https://github.com/BobTheBuidler/mypy…
BobTheBuidler Sep 17, 2025
1668d8a
fix syntax
BobTheBuidler Sep 17, 2025
35d7250
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 17, 2025
a46630d
fix missing import
BobTheBuidler Sep 17, 2025
063831d
Merge branch 'dict-literals' of https://github.com/BobTheBuidler/mypy…
BobTheBuidler Sep 17, 2025
e456707
fix mypy err
BobTheBuidler Sep 17, 2025
888aa41
fix: TypeErr on python3.9
BobTheBuidler Sep 17, 2025
6c3fd1d
Merge branch 'master' into dict-literals
BobTheBuidler Sep 26, 2025
7784efc
Merge branch 'master' into dict-literals
BobTheBuidler Sep 30, 2025
71192b5
Merge branch 'master' into dict-literals
BobTheBuidler Oct 2, 2025
5199aeb
Merge branch 'master' into dict-literals
BobTheBuidler Oct 4, 2025
cb31876
Merge branch 'master' into dict-literals
BobTheBuidler Oct 6, 2025
aa54ec2
Merge branch 'master' into dict-literals
BobTheBuidler Oct 14, 2025
e9297b7
Update expression.py
BobTheBuidler Oct 14, 2025
778e51f
Update expression.py
BobTheBuidler Oct 14, 2025
528ea6f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 14, 2025
63f6538
Update expression.py
BobTheBuidler Oct 14, 2025
9772e12
Update expression.py
BobTheBuidler Oct 14, 2025
4f13ace
Update expression.py
BobTheBuidler Oct 14, 2025
db9884c
Update expression.py
BobTheBuidler Oct 15, 2025
e3d94ef
Merge branch 'master' into dict-literals
BobTheBuidler Oct 15, 2025
af20665
Merge branch 'master' into dict-literals
BobTheBuidler Oct 22, 2025
e7d1f11
Merge branch 'master' into dict-literals
BobTheBuidler Dec 8, 2025
6f3a03c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Dec 8, 2025
09e8aab
Merge branch 'master' into dict-literals
BobTheBuidler Dec 11, 2025
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
13 changes: 11 additions & 2 deletions mypyc/codegen/literals.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,16 @@
# Supported Python literal types. All tuple / frozenset / dict items must have supported
# literal types as well, but we can't represent the type precisely.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a reason we can't represent the type precisely? I think with a HashableLiteralValue defined before LiteralValue, we can do it pretty cleanly

LiteralValue = Union[
str, bytes, int, bool, float, complex, tuple[object, ...], frozenset[object], dict[object, object], None
str,
bytes,
int,
bool,
float,
complex,
tuple[object, ...],
frozenset[object],
dict[object, object],
None,
]


Expand Down Expand Up @@ -124,7 +133,7 @@ def literal_index(self, value: LiteralValue) -> int:
def make_dict_literal_key(self, value: dict[LiteralValue, LiteralValue]) -> tuple[tuple[LiteralValue, LiteralValue]]:
"""Make a unique key for a literal dict."""
return tuple(value.items())

def num_literals(self) -> int:
# The first three are for None, True and False
return (
Expand Down
14 changes: 10 additions & 4 deletions mypyc/irbuild/expression.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,9 @@
BasicBlock,
Call,
ComparisonOp,
InitStatic,
Integer,
LoadAddress,
LoadLiteral,
LoadStatic,
PrimitiveDescription,
RaiseStandardError,
Register,
Expand Down Expand Up @@ -103,7 +101,12 @@
)
from mypyc.irbuild.specialize import apply_function_specialization, apply_method_specialization
from mypyc.primitives.bytes_ops import bytes_slice_op
from mypyc.primitives.dict_ops import dict_get_item_op, dict_new_op, dict_set_item_op, dict_template_copy_op, exact_dict_set_item_op
from mypyc.primitives.dict_ops import (
dict_get_item_op,
dict_new_op,
dict_template_copy_op,
exact_dict_set_item_op,
)
from mypyc.primitives.generic_ops import iter_op, name_op
from mypyc.primitives.list_ops import list_append_op, list_extend_op, list_slice_op
from mypyc.primitives.misc_ops import ellipsis_op, get_module_dict_op, new_slice_op, type_op
Expand Down Expand Up @@ -1012,7 +1015,9 @@ def _visit_tuple_display(builder: IRBuilder, expr: TupleExpr) -> Value:
return builder.primitive_op(list_tuple_op, [val_as_list], expr.line)


def dict_literal_values(builder: IRBuilder, items: Sequence[tuple[Expression | None, Expression]], line: int) -> "dict | None":
def dict_literal_values(
builder: IRBuilder, items: Sequence[tuple[Expression | None, Expression]], line: int
) -> dict | None:
"""Try to extract a constant dict from a dict literal, recursively staticizing nested dicts.

If all keys and values are deeply immutable and constant (including nested dicts as values),
Expand Down Expand Up @@ -1043,6 +1048,7 @@ def dict_literal_values(builder: IRBuilder, items: Sequence[tuple[Expression | N

return result or None


def transform_dict_expr(builder: IRBuilder, expr: DictExpr) -> Value:
"""First accepts all keys and values, then makes a dict out of them.

Expand Down
1 change: 0 additions & 1 deletion mypyc/test-data/irbuild-dict.test
Original file line number Diff line number Diff line change
Expand Up @@ -603,4 +603,3 @@ L0:
r5 = CPyDict_Build(1, r0, r4)
d = r5
return 1

Loading