Skip to content

Commit 949d436

Browse files
committed
fix: python type bugs
1 parent a52e400 commit 949d436

File tree

9 files changed

+82
-44
lines changed

9 files changed

+82
-44
lines changed

examples/typegraphs/metagen/py/fdk.py

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,32 @@
22
# to be generated again on subsequent metagen runs.
33

44
import typing
5-
import io
6-
import re
7-
import uuid
85
import dataclasses as dc
6+
import http.client as http_c
7+
import io
98
import json
9+
import mimetypes
10+
import re
1011
import urllib
11-
import urllib.request as request
1212
import urllib.error
13-
import http.client as http_c
14-
import mimetypes
13+
import urllib.request as request
14+
import uuid
1515
from abc import ABC, abstractmethod
1616

1717

1818
class Ctx:
1919
def __init__(
20-
self, binding: "HostcallBinding", qg: "QueryGraph", host: "HostcallTransport"
20+
self,
21+
binding: "HostcallBinding",
22+
qg: "QueryGraph",
23+
host: "HostcallTransport",
2124
):
22-
self.gql = binding
25+
self.__binding = binding
2326
self.qg = qg
2427
self.host = host
25-
pass
2628

27-
pass
29+
def gql(self, query: str, variables: typing.Mapping):
30+
return self.__binding(query, dict(variables))
2831

2932

3033
def selection_to_nodes(
@@ -38,7 +41,7 @@ def selection_to_nodes(
3841
raise Exception(
3942
f"selection field '_' should be of type SelectionFlags but found {type(sub_flags)}"
4043
)
41-
select_all = True if sub_flags is not None and sub_flags.select_all else False
44+
select_all = bool(sub_flags and sub_flags.select_all)
4245
found_nodes = set(selection.keys())
4346
for node_name, meta_fn in metas.items():
4447
found_nodes.discard(node_name)
@@ -524,10 +527,8 @@ def convert_query_node_gql(
524527
out += f" {{ {sub_node_list}}}"
525528
elif isinstance(node.sub_nodes, list):
526529
sub_node_list = ""
527-
for node in node.sub_nodes:
528-
sub_node_list += (
529-
f"{convert_query_node_gql(ty_to_gql_ty_map, node, variables, files)} "
530-
)
530+
for sub_node in node.sub_nodes:
531+
sub_node_list += f"{convert_query_node_gql(ty_to_gql_ty_map, sub_node, variables, files)} "
531532
out += f" {{ {sub_node_list}}}"
532533
return out
533534

@@ -630,8 +631,10 @@ def build_req(
630631
doc: str,
631632
variables: typing.Dict[str, typing.Any],
632633
opts: typing.Optional[GraphQLTransportOptions] = None,
633-
files: typing.Dict[str, File] = {},
634+
files: typing.Optional[typing.Dict[str, File]] = None,
634635
):
636+
if files is None:
637+
files = {}
635638
headers = {}
636639
headers.update(self.opts.headers)
637640
if opts:
@@ -688,8 +691,8 @@ def fetch(
688691
self,
689692
doc: str,
690693
variables: typing.Dict[str, typing.Any],
691-
opts: GraphQLTransportOptions | None,
692-
files: typing.Dict[str, File] = {},
694+
opts: typing.Optional[GraphQLTransportOptions],
695+
files: typing.Optional[typing.Dict[str, File]] = None,
693696
) -> typing.Any: ...
694697

695698
@typing.overload
@@ -753,8 +756,10 @@ def fetch(
753756
doc: str,
754757
variables: typing.Dict[str, typing.Any],
755758
opts: typing.Optional[GraphQLTransportOptions],
756-
files: typing.Dict[str, File] = {},
759+
files: typing.Optional[typing.Dict[str, File]] = None,
757760
):
761+
if files is None:
762+
files = {}
758763
req = self.build_req(doc, variables, opts, files)
759764
try:
760765
with request.urlopen(
@@ -781,7 +786,7 @@ def fetch(
781786
)
782787
)
783788
except urllib.error.URLError as err:
784-
raise Exception(f"URL error: {err.reason}")
789+
raise Exception(f"URL error: {err.reason}") from err
785790

786791
@typing.overload
787792
def prepare_query(
@@ -859,10 +864,13 @@ def fetch(
859864
doc: str,
860865
variables: typing.Dict[str, typing.Any],
861866
opts: typing.Optional[GraphQLTransportOptions],
862-
files: typing.Dict[str, File] = {},
867+
files: typing.Optional[typing.Dict[str, File]] = None,
863868
):
864869
_ = opts
865870

871+
if files is None:
872+
files = {}
873+
866874
if len(files) > 0:
867875
raise Exception("no support for file upload on HostcallTransport")
868876

@@ -1059,6 +1067,7 @@ def RemixTrack():
10591067
"release_time": Idv3ReleaseTimeStringDatetime,
10601068
"mp3_url": Idv3Mp3UrlStringUri,
10611069
},
1070+
total=False,
10621071
)
10631072

10641073

src/metagen/src/client_py/static/client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -679,7 +679,7 @@ def fetch(
679679
self,
680680
doc: str,
681681
variables: typing.Dict[str, typing.Any],
682-
opts: GraphQLTransportOptions | None,
682+
opts: typing.Optional[GraphQLTransportOptions],
683683
files: typing.Optional[typing.Dict[str, File]] = None,
684684
) -> typing.Any: ...
685685

src/metagen/src/client_py/types.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,17 @@ impl PyTypeRenderer {
3030
) -> std::fmt::Result {
3131
writeln!(dest, r#"{ty_name} = typing.TypedDict("{ty_name}", {{"#)?;
3232
for (name, (ty_name, optional)) in props.into_iter() {
33+
// FIXME: NotRequired is only avail on python 3.11
3334
if optional {
34-
writeln!(dest, r#" "{name}": typing.NotRequired[{ty_name}],"#)?;
35+
// writeln!(dest, r#" "{name}": typing.NotRequired[{ty_name}],"#)?;
36+
writeln!(dest, r#" "{name}": {ty_name},"#)?;
3537
} else {
3638
writeln!(dest, r#" "{name}": {ty_name},"#)?;
3739
}
3840
// writeln!(dest, r#" "{name}": {ty_name},"#)?;
3941
}
40-
writeln!(dest, "}})")?;
42+
// FIXME: all fields are optional due to py 3.9 TypedDict limitations
43+
writeln!(dest, "}}, total=False)")?;
4144
writeln!(dest)?;
4245
Ok(())
4346
}

tests/internal/py/fdk.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -691,7 +691,7 @@ def fetch(
691691
self,
692692
doc: str,
693693
variables: typing.Dict[str, typing.Any],
694-
opts: GraphQLTransportOptions | None,
694+
opts: typing.Optional[GraphQLTransportOptions],
695695
files: typing.Optional[typing.Dict[str, File]] = None,
696696
) -> typing.Any: ...
697697

@@ -1070,6 +1070,7 @@ def RootRemoteSumPyFn():
10701070
"first": float,
10711071
"second": float,
10721072
},
1073+
total=False,
10731074
)
10741075

10751076

tests/metagen/typegraphs/identities/py/fdk.py

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -691,7 +691,7 @@ def fetch(
691691
self,
692692
doc: str,
693693
variables: typing.Dict[str, typing.Any],
694-
opts: GraphQLTransportOptions | None,
694+
opts: typing.Optional[GraphQLTransportOptions],
695695
files: typing.Optional[typing.Dict[str, File]] = None,
696696
) -> typing.Any: ...
697697

@@ -1358,13 +1358,15 @@ def RsProxyPrimitives():
13581358
"float": PrimitivesFloatFloat,
13591359
"boolean": PrimitivesBooleanBoolean,
13601360
},
1361+
total=False,
13611362
)
13621363

13631364
PrimitivesArgs = typing.TypedDict(
13641365
"PrimitivesArgs",
13651366
{
13661367
"data": Primitives,
13671368
},
1369+
total=False,
13681370
)
13691371

13701372
CompositesOptPrimitivesStrStringOptional = typing.Union[PrimitivesStrString, None]
@@ -1374,6 +1376,7 @@ def RsProxyPrimitives():
13741376
{
13751377
"branch2": PrimitivesStrString,
13761378
},
1379+
total=False,
13771380
)
13781381

13791382
CompositesEitherEither = typing.Union[
@@ -1405,36 +1408,40 @@ def RsProxyPrimitives():
14051408
Composites = typing.TypedDict(
14061409
"Composites",
14071410
{
1408-
"opt": typing.NotRequired[CompositesOptPrimitivesStrStringOptional],
1411+
"opt": CompositesOptPrimitivesStrStringOptional,
14091412
"either": CompositesEitherEither,
14101413
"union": CompositesUnionUnion,
14111414
"list": CompositesListPrimitivesStrStringList,
14121415
},
1416+
total=False,
14131417
)
14141418

14151419
CompositesArgs = typing.TypedDict(
14161420
"CompositesArgs",
14171421
{
14181422
"data": Composites,
14191423
},
1424+
total=False,
14201425
)
14211426

14221427
Branch33ATo1Cycles1Optional = typing.Union["Cycles1", None]
14231428

14241429
Branch33A = typing.TypedDict(
14251430
"Branch33A",
14261431
{
1427-
"phantom3a": typing.NotRequired[CompositesOptPrimitivesStrStringOptional],
1428-
"to1": typing.NotRequired[Branch33ATo1Cycles1Optional],
1432+
"phantom3a": CompositesOptPrimitivesStrStringOptional,
1433+
"to1": Branch33ATo1Cycles1Optional,
14291434
},
1435+
total=False,
14301436
)
14311437

14321438
Branch33B = typing.TypedDict(
14331439
"Branch33B",
14341440
{
1435-
"phantom3b": typing.NotRequired[CompositesOptPrimitivesStrStringOptional],
1436-
"to2": typing.NotRequired["Cycles1To2Cycles2Optional"],
1441+
"phantom3b": CompositesOptPrimitivesStrStringOptional,
1442+
"to2": "Cycles1To2Cycles2Optional",
14371443
},
1444+
total=False,
14381445
)
14391446

14401447
Cycles3 = typing.Union[
@@ -1460,54 +1467,60 @@ def RsProxyPrimitives():
14601467
Cycles1 = typing.TypedDict(
14611468
"Cycles1",
14621469
{
1463-
"phantom1": typing.NotRequired[CompositesOptPrimitivesStrStringOptional],
1464-
"to2": typing.NotRequired["Cycles1To2Cycles2Optional"],
1465-
"list3": typing.NotRequired[Cycles1List3Cycles1List3Cycles3ListOptional],
1470+
"phantom1": CompositesOptPrimitivesStrStringOptional,
1471+
"to2": "Cycles1To2Cycles2Optional",
1472+
"list3": Cycles1List3Cycles1List3Cycles3ListOptional,
14661473
},
1474+
total=False,
14671475
)
14681476

14691477
Cycles1Args = typing.TypedDict(
14701478
"Cycles1Args",
14711479
{
14721480
"data": Cycles1,
14731481
},
1482+
total=False,
14741483
)
14751484

14761485
SimpleCycles3To1SimpleCycles1Optional = typing.Union["SimpleCycles1", None]
14771486

14781487
SimpleCycles3 = typing.TypedDict(
14791488
"SimpleCycles3",
14801489
{
1481-
"phantom3": typing.NotRequired[CompositesOptPrimitivesStrStringOptional],
1482-
"to1": typing.NotRequired[SimpleCycles3To1SimpleCycles1Optional],
1490+
"phantom3": CompositesOptPrimitivesStrStringOptional,
1491+
"to1": SimpleCycles3To1SimpleCycles1Optional,
14831492
},
1493+
total=False,
14841494
)
14851495

14861496
SimpleCycles2To3SimpleCycles3Optional = typing.Union[SimpleCycles3, None]
14871497

14881498
SimpleCycles2 = typing.TypedDict(
14891499
"SimpleCycles2",
14901500
{
1491-
"phantom2": typing.NotRequired[CompositesOptPrimitivesStrStringOptional],
1492-
"to3": typing.NotRequired[SimpleCycles2To3SimpleCycles3Optional],
1501+
"phantom2": CompositesOptPrimitivesStrStringOptional,
1502+
"to3": SimpleCycles2To3SimpleCycles3Optional,
14931503
},
1504+
total=False,
14941505
)
14951506

14961507
SimpleCycles1To2SimpleCycles2Optional = typing.Union[SimpleCycles2, None]
14971508

14981509
SimpleCycles1 = typing.TypedDict(
14991510
"SimpleCycles1",
15001511
{
1501-
"phantom1": typing.NotRequired[CompositesOptPrimitivesStrStringOptional],
1502-
"to2": typing.NotRequired["SimpleCycles1To2SimpleCycles2Optional"],
1512+
"phantom1": CompositesOptPrimitivesStrStringOptional,
1513+
"to2": "SimpleCycles1To2SimpleCycles2Optional",
15031514
},
1515+
total=False,
15041516
)
15051517

15061518
SimpleCycles1Args = typing.TypedDict(
15071519
"SimpleCycles1Args",
15081520
{
15091521
"data": SimpleCycles1,
15101522
},
1523+
total=False,
15111524
)
15121525

15131526

tests/metagen/typegraphs/sample/py/client.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -678,7 +678,7 @@ def fetch(
678678
self,
679679
doc: str,
680680
variables: typing.Dict[str, typing.Any],
681-
opts: GraphQLTransportOptions | None,
681+
opts: typing.Optional[GraphQLTransportOptions],
682682
files: typing.Optional[typing.Dict[str, File]] = None,
683683
) -> typing.Any: ...
684684

@@ -1124,20 +1124,23 @@ def RootIdentityUpdateFn():
11241124
"slug": str,
11251125
"title": str,
11261126
},
1127+
total=False,
11271128
)
11281129

11291130
RootCompositeArgsFnInput = typing.TypedDict(
11301131
"RootCompositeArgsFnInput",
11311132
{
11321133
"id": str,
11331134
},
1135+
total=False,
11341136
)
11351137

11361138
RootIdentityFnInput = typing.TypedDict(
11371139
"RootIdentityFnInput",
11381140
{
11391141
"input": int,
11401142
},
1143+
total=False,
11411144
)
11421145

11431146
UserEmailStringEmail = str
@@ -1151,6 +1154,7 @@ def RootIdentityUpdateFn():
11511154
"email": UserEmailStringEmail,
11521155
"posts": UserPostsPostList,
11531156
},
1157+
total=False,
11541158
)
11551159

11561160
RootScalarUnionFnOutput = typing.Union[
@@ -1178,6 +1182,7 @@ def RootIdentityUpdateFn():
11781182
{
11791183
"inner": int,
11801184
},
1185+
total=False,
11811186
)
11821187

11831188
RootNestedCompositeFnOutputCompositeStruct = typing.TypedDict(
@@ -1186,13 +1191,15 @@ def RootIdentityUpdateFn():
11861191
"value": int,
11871192
"nested": RootNestedCompositeFnOutputCompositeStructNestedStruct,
11881193
},
1194+
total=False,
11891195
)
11901196

11911197
RootNestedCompositeFnOutputListStruct = typing.TypedDict(
11921198
"RootNestedCompositeFnOutputListStruct",
11931199
{
11941200
"value": int,
11951201
},
1202+
total=False,
11961203
)
11971204

11981205
RootNestedCompositeFnOutputListRootNestedCompositeFnOutputListStructList = typing.List[
@@ -1206,6 +1213,7 @@ def RootIdentityUpdateFn():
12061213
"composite": RootNestedCompositeFnOutputCompositeStruct,
12071214
"list": RootNestedCompositeFnOutputListRootNestedCompositeFnOutputListStructList,
12081215
},
1216+
total=False,
12091217
)
12101218

12111219

0 commit comments

Comments
 (0)