Skip to content

Commit de14bfb

Browse files
authored
Merge branch 'main' into fix-raw-yaml-decode-error
2 parents 028b938 + 3f67b34 commit de14bfb

13 files changed

Lines changed: 252 additions & 23 deletions

File tree

‎.github/workflows/ci.yml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ on:
2323
- .github/workflows/*
2424
- .cibuildwheel.toml
2525
- .justfile
26+
- scripts/**
2627
# Changes to testing
2728
- tests/**
2829
- .codecov.yml

‎.justfile‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ test-typing: (
9090
env-run "test" "pyright tests/typing"
9191
) (
9292
env-run "test" "pyrefly check tests/typing"
93+
) (
94+
env-run "test" "python scripts/stubtest.py"
9395
)
9496

9597
# Run doctests.

‎docs/changelog.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
- Fix the `msgspec.json.decode` docstring to say `dec_hook` should raise
3838
`NotImplementedError` for unsupported types, not `TypeError` ({issue}`774`).
3939
- Fix backing type declaration of `Ext.code` ({pr}`1135`).
40+
- Fix `NameError` when creating a `Struct` with an unquoted forward
41+
reference on Python 3.14 ({issue}`1165`).
4042
- msgspec moved to the [msgspec GitHub organization](https://github.com/msgspec/msgspec);
4143
documentation now lives at [msgspec.dev](https://msgspec.dev) (repository
4244
references updated in {pr}`1045`).

‎scripts/stubtest.py‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
"""Run stubtest against the installed msgspec package.
2+
3+
Entries that apply to every supported Python version live in
4+
`tests/typing/stubtest_allowlist.txt`. Findings that only exist from a given
5+
version onwards go in `tests/typing/stubtest_allowlist_py<major><minor>_plus.txt`,
6+
and every such file whose version is at or below the running interpreter is
7+
passed too.
8+
9+
stubtest runs without `--ignore-unused-allowlist`, so an entry that stops
10+
matching is reported instead of silently lingering.
11+
"""
12+
13+
import re
14+
import subprocess
15+
import sys
16+
from pathlib import Path
17+
18+
ALLOWLIST_DIR = Path(__file__).parents[1] / "tests" / "typing"
19+
VERSIONED_ALLOWLIST = re.compile(r"^stubtest_allowlist_py(\d)(\d+)_plus\.txt$")
20+
21+
22+
def allowlists():
23+
yield ALLOWLIST_DIR / "stubtest_allowlist.txt"
24+
25+
versioned = []
26+
for path in ALLOWLIST_DIR.iterdir():
27+
match = VERSIONED_ALLOWLIST.match(path.name)
28+
if match is not None:
29+
version = (int(match.group(1)), int(match.group(2)))
30+
if version <= sys.version_info[:2]:
31+
versioned.append((version, path))
32+
33+
for _, path in sorted(versioned):
34+
yield path
35+
36+
37+
def main():
38+
command = [sys.executable, "-m", "mypy.stubtest", "msgspec"]
39+
for allowlist in allowlists():
40+
command.extend(["--allowlist", str(allowlist)])
41+
command.extend(sys.argv[1:])
42+
return subprocess.call(command)
43+
44+
45+
if __name__ == "__main__":
46+
raise SystemExit(main())

‎src/msgspec/__init__.pyi‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,13 @@ from typing import (
1515
overload,
1616
)
1717

18-
from typing_extensions import Buffer, Self, TypeAlias, dataclass_transform
18+
from typing_extensions import (
19+
Buffer,
20+
Self,
21+
TypeAlias,
22+
dataclass_transform,
23+
disjoint_base,
24+
)
1925

2026
from . import inspect, json, msgpack, structs, toml, yaml
2127

@@ -26,6 +32,7 @@ from . import inspect, json, msgpack, structs, toml, yaml
2632
# https://github.com/python/typeshed/blob/17bde1bd5e556de001adde3c2f340ba1c3581bd2/stdlib/abc.pyi#L14-L19
2733
_SM = TypeVar("_SM", bound="StructMeta")
2834

35+
@disjoint_base
2936
class StructMeta(type):
3037
__struct_fields__: ClassVar[tuple[str, ...]]
3138
__struct_defaults__: ClassVar[tuple[Any, ...]]

‎src/msgspec/_core.c‎

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,7 @@ typedef struct {
530530
PyObject *concrete_types;
531531
PyObject *get_type_hints;
532532
PyObject *get_class_annotations;
533+
PyObject *call_annotate_forwardref;
533534
PyObject *get_typeddict_info;
534535
PyObject *get_dataclass_info;
535536
PyObject *convert_generic_alias;
@@ -6120,16 +6121,12 @@ structmeta_collect_fields(StructMetaInfo *info, MsgspecState *mod, bool kwonly)
61206121
Py_DECREF(annotate);
61216122
return 0;
61226123
}
6123-
PyObject *format = PyLong_FromLong(1); /* annotationlib.Format.VALUE */
6124-
if (format == NULL) {
6125-
Py_DECREF(annotate);
6126-
return -1;
6127-
}
6128-
annotations = PyObject_CallOneArg(
6129-
annotate, format
6130-
);
6124+
/* Use Format.FORWARDREF so unresolved names do not raise NameError
6125+
* while the class body is still executing. See PEP 649 / annotationlib:
6126+
* https://docs.python.org/3/library/annotationlib.html#using-annotations-in-a-metaclass
6127+
*/
6128+
annotations = PyObject_CallOneArg(mod->call_annotate_forwardref, annotate);
61316129
Py_DECREF(annotate);
6132-
Py_DECREF(format);
61336130
if (annotations == NULL) {
61346131
return -1;
61356132
}
@@ -9910,7 +9907,7 @@ encoder_encode_into_common(
99109907
}
99119908

99129909
PyDoc_STRVAR(Encoder_encode__doc__,
9913-
"encode(self, obj)\n"
9910+
"encode(self, obj, /)\n"
99149911
"--\n"
99159912
"\n"
99169913
"Serialize an object to bytes.\n"
@@ -13804,7 +13801,7 @@ static PyTypeObject Encoder_Type = {
1380413801
};
1380513802

1380613803
PyDoc_STRVAR(msgspec_msgpack_encode__doc__,
13807-
"msgpack_encode(obj, *, enc_hook=None, order=None)\n"
13804+
"msgpack_encode(obj, /, *, enc_hook=None, order=None)\n"
1380813805
"--\n"
1380913806
"\n"
1381013807
"Serialize an object as MessagePack.\n"
@@ -14891,7 +14888,7 @@ JSONEncoder_encode(Encoder *self, PyObject *const *args, Py_ssize_t nargs)
1489114888
}
1489214889

1489314890
PyDoc_STRVAR(JSONEncoder_encode_lines__doc__,
14894-
"encode_lines(self, items)\n"
14891+
"encode_lines(self, items, /)\n"
1489514892
"--\n"
1489614893
"\n"
1489714894
"Encode an iterable of items as newline-delimited JSON, one item per line.\n"
@@ -14997,7 +14994,7 @@ static PyTypeObject JSONEncoder_Type = {
1499714994
};
1499814995

1499914996
PyDoc_STRVAR(msgspec_json_encode__doc__,
15000-
"json_encode(obj, *, enc_hook=None, order=None)\n"
14997+
"json_encode(obj, /, *, enc_hook=None, order=None)\n"
1500114998
"--\n"
1500214999
"\n"
1500315000
"Serialize an object as JSON.\n"
@@ -16762,7 +16759,7 @@ mpack_decode(
1676216759
}
1676316760

1676416761
PyDoc_STRVAR(Decoder_decode__doc__,
16765-
"decode(self, buf)\n"
16762+
"decode(self, buf, /)\n"
1676616763
"--\n"
1676716764
"\n"
1676816765
"Deserialize an object from MessagePack.\n"
@@ -16845,7 +16842,7 @@ static PyTypeObject Decoder_Type = {
1684516842

1684616843

1684716844
PyDoc_STRVAR(msgspec_msgpack_decode__doc__,
16848-
"msgpack_decode(buf, *, type='Any', strict=True, dec_hook=None, ext_hook=None)\n"
16845+
"msgpack_decode(buf, /, *, type='Any', strict=True, dec_hook=None, ext_hook=None)\n"
1684916846
"--\n"
1685016847
"\n"
1685116848
"Deserialize an object from MessagePack.\n"
@@ -19734,7 +19731,7 @@ msgspec_json_format(PyObject *self, PyObject *args, PyObject *kwargs)
1973419731

1973519732

1973619733
PyDoc_STRVAR(JSONDecoder_decode__doc__,
19737-
"decode(self, buf)\n"
19734+
"decode(self, buf, /)\n"
1973819735
"--\n"
1973919736
"\n"
1974019737
"Deserialize an object from JSON.\n"
@@ -19791,7 +19788,7 @@ JSONDecoder_decode(JSONDecoder *self, PyObject *const *args, Py_ssize_t nargs)
1979119788
}
1979219789

1979319790
PyDoc_STRVAR(JSONDecoder_decode_lines__doc__,
19794-
"decode_lines(self, buf)\n"
19791+
"decode_lines(self, buf, /)\n"
1979519792
"--\n"
1979619793
"\n"
1979719794
"Decode a list of items from newline-delimited JSON.\n"
@@ -19922,7 +19919,7 @@ static PyTypeObject JSONDecoder_Type = {
1992219919
};
1992319920

1992419921
PyDoc_STRVAR(msgspec_json_decode__doc__,
19925-
"json_decode(buf, *, type='Any', strict=True, dec_hook=None)\n"
19922+
"json_decode(buf, /, *, type='Any', strict=True, dec_hook=None)\n"
1992619923
"--\n"
1992719924
"\n"
1992819925
"Deserialize an object from JSON.\n"
@@ -20937,7 +20934,14 @@ convert_int(
2093720934
return ms_decode_int_enum_or_literal_pyint(obj, type, path);
2093820935
}
2093920936
else if (type->types & MS_TYPE_FLOAT) {
20940-
return ms_decode_float(PyLong_AsDouble(obj), type, path);
20937+
double val = PyLong_AsDouble(obj);
20938+
if (val == -1.0 && PyErr_Occurred()) {
20939+
/* `obj` is out of range for a C double (PyLong_AsDouble sets
20940+
* OverflowError but still returns -1.0); without this check
20941+
* that error leaks past this function as a SystemError. */
20942+
return ms_error_with_path("Number out of range%U", path);
20943+
}
20944+
return ms_decode_float(val, type, path);
2094120945
}
2094220946
else if (
2094320947
type->types & MS_TYPE_DECIMAL
@@ -22705,6 +22709,7 @@ msgspec_clear(PyObject *m)
2270522709
Py_CLEAR(st->concrete_types);
2270622710
Py_CLEAR(st->get_type_hints);
2270722711
Py_CLEAR(st->get_class_annotations);
22712+
Py_CLEAR(st->call_annotate_forwardref);
2270822713
Py_CLEAR(st->get_typeddict_info);
2270922714
Py_CLEAR(st->get_dataclass_info);
2271022715
Py_CLEAR(st->rebuild);
@@ -22779,6 +22784,7 @@ msgspec_traverse(PyObject *m, visitproc visit, void *arg)
2277922784
Py_VISIT(st->concrete_types);
2278022785
Py_VISIT(st->get_type_hints);
2278122786
Py_VISIT(st->get_class_annotations);
22787+
Py_VISIT(st->call_annotate_forwardref);
2278222788
Py_VISIT(st->get_typeddict_info);
2278322789
Py_VISIT(st->get_dataclass_info);
2278422790
Py_VISIT(st->rebuild);
@@ -22981,6 +22987,7 @@ PyInit__core(void)
2298122987
SET_REF(concrete_types, "_CONCRETE_TYPES");
2298222988
SET_REF(get_type_hints, "get_type_hints");
2298322989
SET_REF(get_class_annotations, "get_class_annotations");
22990+
SET_REF(call_annotate_forwardref, "call_annotate_forwardref");
2298422991
SET_REF(get_typeddict_info, "get_typeddict_info");
2298522992
SET_REF(get_dataclass_info, "get_dataclass_info");
2298622993
SET_REF(typing_annotated_alias, "_AnnotatedAlias");

‎src/msgspec/_utils.py‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,29 @@ def _eval_type(t, globalns, localns):
5353

5454

5555
if sys.version_info >= (3, 14):
56-
from annotationlib import get_annotations as _get_class_annotations
56+
from annotationlib import (
57+
Format as _AnnotationFormat,
58+
call_annotate_function as _call_annotate_function,
59+
get_annotations as _get_class_annotations,
60+
)
61+
62+
def call_annotate_forwardref(annotate):
63+
"""Evaluate a class annotate function without requiring names to exist.
64+
65+
Metaclasses should use ``Format.FORWARDREF`` (via
66+
``call_annotate_function``, which implements the format when the
67+
annotate function only supports ``VALUE``).
68+
"""
69+
return _call_annotate_function(annotate, _AnnotationFormat.FORWARDREF)
70+
5771
else:
5872

5973
def _get_class_annotations(cls):
6074
return cls.__dict__.get("__annotations__", {})
6175

76+
def call_annotate_forwardref(annotate):
77+
return annotate(1) # annotationlib.Format.VALUE
78+
6279

6380
def _apply_params(obj, mapping):
6481
if isinstance(obj, typing.TypeVar):

‎src/msgspec/json.pyi‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,6 @@ def schema_components(
112112
ref_template: str = "#/$defs/{name}",
113113
) -> tuple[tuple[dict[str, Any], ...], dict[str, Any]]: ...
114114
@overload
115-
def format(buf: str, /, *, indent: int = 2) -> str: ...
115+
def format(buf: str, *, indent: int = 2) -> str: ...
116116
@overload
117-
def format(buf: Buffer, /, *, indent: int = 2) -> bytes: ...
117+
def format(buf: Buffer, *, indent: int = 2) -> bytes: ...

‎src/msgspec/structs.pyi‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,16 @@ from typing import Any, TypeVar, final
22

33
from . import NODEFAULT, Struct
44

5+
__all__ = (
6+
"FieldInfo",
7+
"StructConfig",
8+
"asdict",
9+
"astuple",
10+
"fields",
11+
"force_setattr",
12+
"replace",
13+
)
14+
515
_S = TypeVar("_S", bound=Struct)
616

717
def replace(struct: _S, /, **changes: Any) -> _S: ...
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Allowlist for `python -m mypy.stubtest msgspec`, run via scripts/stubtest.py.
2+
#
3+
# Each entry is a regex matched against the fully-qualified name of a stubtest
4+
# finding. Entries here are known false positives or intentional gaps; real
5+
# stub/runtime drift should be fixed rather than added here.
6+
#
7+
# This file holds entries that apply to every supported Python version. Entries
8+
# that only surface from a given version onwards belong in
9+
# stubtest_allowlist_py<major><minor>_plus.txt instead. stubtest runs without
10+
# --ignore-unused-allowlist, so an entry that stops matching fails the run.
11+
12+
# msgspec.Struct subclasses get a per-class __init__ synthesized in C. stubtest
13+
# can't see through it, so every Struct subtype reports an __init__ mismatch
14+
# against the generic stub. Same story for the C types with a custom __new__.
15+
msgspec\.Struct\.__init__
16+
msgspec\.Meta\.__init__
17+
msgspec\.Meta\.__new__
18+
msgspec\.structs\.FieldInfo\.__init__
19+
msgspec\.inspect\..*\.__init__
20+
msgspec\.msgpack\.Ext\.__init__
21+
msgspec\.msgpack\.Ext\.__new__
22+
msgspec\.UnsetType\.__new__
23+
24+
# Private, intentionally unstubbed helper module.
25+
msgspec\._utils\..*

0 commit comments

Comments
 (0)