Skip to content

Commit 3800b50

Browse files
alexmalyshevmeta-codesync[bot]
authored andcommitted
Handle kw_only flag for dataclasses in Static Python
Summary: Doesn't handle it *well* but at least it no longer amounts to an exception. Reviewed By: martindemello Differential Revision: D96499729 fbshipit-source-id: 7accdef8aeb288567d4dc41cf054a12ab2b6913c
1 parent 9d08da5 commit 3800b50

2 files changed

Lines changed: 43 additions & 1 deletion

File tree

cinderx/PythonLib/cinderx/compiler/static/types.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5272,6 +5272,14 @@ def __init__(self, type_env: TypeEnvironment) -> None:
52725272
False,
52735273
ParamStyle.KWONLY,
52745274
),
5275+
Parameter(
5276+
"kw_only",
5277+
7,
5278+
ResolvedTypeRef(type_env.bool),
5279+
True,
5280+
False,
5281+
ParamStyle.KWONLY,
5282+
),
52755283
]
52765284
super().__init__(
52775285
type_env.function,
@@ -5321,6 +5329,7 @@ def resolve_decorate_class(
53215329
"order": False,
53225330
"unsafe_hash": False,
53235331
"frozen": False,
5332+
"kw_only": False,
53245333
}
53255334

53265335
for kw in decorator.keywords:
@@ -5582,6 +5591,7 @@ def __init__(
55825591
order: bool = False,
55835592
unsafe_hash: bool = False,
55845593
frozen: bool = False,
5594+
kw_only: bool = False,
55855595
) -> None:
55865596
super().__init__(
55875597
type_name=klass.type_name,
@@ -5602,6 +5612,7 @@ def __init__(
56025612
self.order = order
56035613
self.unsafe_hash = unsafe_hash
56045614
self.frozen = frozen
5615+
self.kw_only = kw_only
56055616

56065617
self.fields: dict[str, DataclassField] = {}
56075618

@@ -5824,14 +5835,15 @@ def finish_bind(self, module: ModuleTable, klass: Class | None) -> Value | None:
58245835
default = ast.Name("_HAS_DEFAULT_FACTORY", ast.Load())
58255836
else:
58265837
default = None
5838+
58275839
init_params.append(
58285840
Parameter(
58295841
name,
58305842
len(init_params),
58315843
field.unwrapped_ref,
58325844
has_default,
58335845
default,
5834-
ParamStyle.NORMAL,
5846+
ParamStyle.KWONLY if self.kw_only else ParamStyle.NORMAL,
58355847
)
58365848
)
58375849

cinderx/PythonLib/test_cinderx/test_compiler/test_static/dataclass.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1858,3 +1858,33 @@ def x(c: C):
18581858
a = mod.C("foo")
18591859
for _ in range(256):
18601860
self.assertEqual(mod.x(a), "fooFalseNoneNone")
1861+
1862+
def test_dataclass_kw_only(self) -> None:
1863+
codestr = """
1864+
from dataclasses import dataclass
1865+
1866+
@dataclass(kw_only=True)
1867+
class C:
1868+
x: int
1869+
y: float
1870+
1871+
c = C(x=10, y=2.0)
1872+
"""
1873+
with self.in_module(codestr) as mod:
1874+
self.assertEqual(mod.c.x, 10)
1875+
1876+
def test_dataclass_kw_only_wrong(self) -> None:
1877+
codestr = """
1878+
from dataclasses import dataclass
1879+
1880+
@dataclass(kw_only=True)
1881+
class C:
1882+
x: int
1883+
y: float
1884+
1885+
c = C(10, y=2.0)
1886+
"""
1887+
1888+
# TODO(T259691972): This should raise a TypedSyntaxError!
1889+
with self.in_module(codestr) as mod:
1890+
self.assertEqual(mod.c.x, 10)

0 commit comments

Comments
 (0)