Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
cce8669
[mypyc] fix: reject invalid `mypyc_attr` args
BobTheBuidler Oct 1, 2025
d4631ac
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 1, 2025
0cc6c5d
fix: add key 'free_list_len'
BobTheBuidler Oct 1, 2025
4aefb1b
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 1, 2025
3c27112
fix mypy errs
BobTheBuidler Oct 1, 2025
c2f3d36
Update util.py
BobTheBuidler Oct 1, 2025
0e1c740
Update util.py
BobTheBuidler Oct 1, 2025
0de2627
Update util.py
BobTheBuidler Oct 1, 2025
4a88449
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 1, 2025
7932b81
Update util.py
BobTheBuidler Oct 1, 2025
593c5ee
Update util.py
BobTheBuidler Oct 1, 2025
9d12459
fix mypy err
BobTheBuidler Oct 1, 2025
f7468d9
fix mypy err
BobTheBuidler Oct 1, 2025
71473d3
Update util.py
BobTheBuidler Oct 3, 2025
123b740
test error message
BobTheBuidler Oct 3, 2025
3e98dce
Update irbuild-classes.test
BobTheBuidler Oct 3, 2025
dfe19bb
add note to tests
BobTheBuidler Oct 3, 2025
16fb0f3
Update irbuild-classes.test
BobTheBuidler Oct 3, 2025
b220df5
Update irbuild-classes.test
BobTheBuidler Oct 3, 2025
cae1c85
Merge branch 'master' into reject-invalid-mypyc-attr
BobTheBuidler Oct 4, 2025
8caceb9
use double quotes
BobTheBuidler Oct 6, 2025
e6670cf
another double quote
BobTheBuidler Oct 6, 2025
c663542
Update util.py
BobTheBuidler Oct 6, 2025
2a0bfbd
Update irbuild-classes.test
BobTheBuidler Oct 6, 2025
ac891b9
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 6, 2025
0c23635
Update util.py
BobTheBuidler Oct 6, 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
Next Next commit
[mypyc] fix: reject invalid mypyc_attr args
  • Loading branch information
BobTheBuidler committed Oct 1, 2025
commit cce8669b0f6915ad67927c3cc52c330ff8320e0b
2 changes: 1 addition & 1 deletion mypyc/irbuild/prepare.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ def prepare_class_def(
ir = mapper.type_to_ir[cdef.info]
info = cdef.info

attrs, attrs_lines = get_mypyc_attrs(cdef)
attrs, attrs_lines = get_mypyc_attrs(cdef, path, errors)
if attrs.get("allow_interpreted_subclasses") is True:
ir.allow_interpreted_subclasses = True
if attrs.get("serializable") is True:
Expand Down
50 changes: 39 additions & 11 deletions mypyc/irbuild/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

from __future__ import annotations

from typing import Any
from typing import Any, Final, FrozenSet, Literal, TypedDict

from typing_extensions import NotRequired

from mypy.nodes import (
ARG_NAMED,
Expand Down Expand Up @@ -31,7 +33,18 @@
from mypy.types import FINAL_DECORATOR_NAMES
from mypyc.errors import Errors

DATACLASS_DECORATORS = {"dataclasses.dataclass", "attr.s", "attr.attrs"}
MYPYC_ATTRS: Final[FrozenSet["MypycAttr"]] = frozenset(["native_class", "allow_interpreted_subclasses", "serializable"])

DATACLASS_DECORATORS: Final = frozenset(["dataclasses.dataclass", "attr.s", "attr.attrs"])


MypycAttr = Literal["native_class", "allow_interpreted_subclasses", "serializable"]

class MypycAttrs(TypedDict):
native_class: NotRequired[bool]
allow_interpreted_subclasses: NotRequired[bool]
serializable: NotRequired[bool]
free_list_len: NotRequired[int]


def is_final_decorator(d: Expression) -> bool:
Expand Down Expand Up @@ -111,22 +124,37 @@ def get_mypyc_attr_call(d: Expression) -> CallExpr | None:
return d
return None


def get_mypyc_attrs(stmt: ClassDef | Decorator) -> tuple[dict[str, Any], dict[str, int]]:

def get_mypyc_attrs(
stmt: ClassDef | Decorator,
path: str,
errors: Errors,
) -> tuple[MypycAttrs, dict[MypycAttr, int]]:
"""Collect all the mypyc_attr attributes on a class definition or a function."""
attrs: dict[str, Any] = {}
lines: dict[str, int] = {}
attrs: MypycAttrs = {}
lines: dict[MypycAttr, int] = {}

def record_unsupported_key(key: str) -> None:
errors.error(f"{key} is not a supported `mypyc_attrs` key.", path, line)
errors.note(f"supported keys: {', '.join(map(repr, sorted(MYPYC_ATTRS)))}", path, line)

for dec in stmt.decorators:
d = get_mypyc_attr_call(dec)
if d:
if d := get_mypyc_attr_call(dec):
line = d.line
for name, arg in zip(d.arg_names, d.args):
if name is None:
if isinstance(arg, StrExpr):
attrs[arg.value] = True
lines[arg.value] = d.line
key = arg.value
if key in MYPYC_ATTRS:
attrs[key] = True
lines[key] = line
else:
record_unsupported_key(key)
elif name not in MYPYC_ATTRS:
record_unsupported_key(name)
else:
attrs[name] = get_mypyc_attr_literal(arg)
lines[name] = d.line
lines[name] = line

return attrs, lines

Expand Down
Loading