75
75
DefType ,
76
76
DefTypeBase ,
77
77
DefTypeClassMethod ,
78
- DefTypeClassMethodArity ,
79
78
DefTypeMember ,
80
79
DefTypeMethod ,
81
80
DefTypeMethodArity ,
82
- DefTypeMethodArityBase ,
83
- DefTypeMethodBase ,
84
81
DefTypeProperty ,
82
+ DefTypePythonMember ,
85
83
DefTypeStaticMethod ,
86
- DefTypeStaticMethodArity ,
87
84
Do ,
88
85
Fn ,
89
86
FnArity ,
125
122
VarRef ,
126
123
Vector as VectorNode ,
127
124
WithMeta ,
125
+ deftype_or_reify_python_member_names ,
128
126
)
129
127
from basilisp .lang .interfaces import IMeta , IRecord , ISeq , IType , IWithMeta
130
128
from basilisp .lang .runtime import Var
@@ -1038,7 +1036,7 @@ def __deftype_classmethod(
1038
1036
method_name : str ,
1039
1037
args : vec .Vector ,
1040
1038
kwarg_support : Optional [KeywordArgSupport ] = None ,
1041
- ) -> DefTypeClassMethodArity :
1039
+ ) -> DefTypeClassMethod :
1042
1040
"""Emit a node for a :classmethod member of a `deftype*` form."""
1043
1041
with ctx .hide_parent_symbol_table (), ctx .new_symbol_table (
1044
1042
method_name , is_context_boundary = True
@@ -1068,7 +1066,7 @@ def __deftype_classmethod(
1068
1066
)
1069
1067
with ctx .new_func_ctx (FunctionContext .CLASSMETHOD ), ctx .expr_pos ():
1070
1068
stmts , ret = _body_ast (ctx , runtime .nthrest (form , 2 ))
1071
- method = DefTypeClassMethodArity (
1069
+ method = DefTypeClassMethod (
1072
1070
form = form ,
1073
1071
name = method_name ,
1074
1072
params = vec .vector (param_nodes ),
@@ -1228,7 +1226,7 @@ def __deftype_staticmethod(
1228
1226
method_name : str ,
1229
1227
args : vec .Vector ,
1230
1228
kwarg_support : Optional [KeywordArgSupport ] = None ,
1231
- ) -> DefTypeStaticMethodArity :
1229
+ ) -> DefTypeStaticMethod :
1232
1230
"""Emit a node for a :staticmethod member of a `deftype*` form."""
1233
1231
with ctx .hide_parent_symbol_table (), ctx .new_symbol_table (
1234
1232
method_name , is_context_boundary = True
@@ -1238,7 +1236,7 @@ def __deftype_staticmethod(
1238
1236
)
1239
1237
with ctx .new_func_ctx (FunctionContext .STATICMETHOD ), ctx .expr_pos ():
1240
1238
stmts , ret = _body_ast (ctx , runtime .nthrest (form , 2 ))
1241
- method = DefTypeStaticMethodArity (
1239
+ method = DefTypeStaticMethod (
1242
1240
form = form ,
1243
1241
name = method_name ,
1244
1242
params = vec .vector (param_nodes ),
@@ -1262,7 +1260,7 @@ def __deftype_staticmethod(
1262
1260
1263
1261
def __deftype_or_reify_prop_or_method_arity ( # pylint: disable=too-many-branches
1264
1262
ctx : AnalyzerContext , form : Union [llist .List , ISeq ], special_form : sym .Symbol
1265
- ) -> Union [DefTypeMethodArityBase , DefTypeProperty ]:
1263
+ ) -> Union [DefTypeMethodArity , DefTypePythonMember ]:
1266
1264
"""Emit either a `deftype*` or `reify*` property node or an arity of a `deftype*`
1267
1265
or `reify*` method.
1268
1266
@@ -1343,9 +1341,9 @@ def __deftype_or_reify_prop_or_method_arity( # pylint: disable=too-many-branche
1343
1341
def __deftype_or_reify_method_node_from_arities ( # pylint: disable=too-many-branches
1344
1342
ctx : AnalyzerContext ,
1345
1343
form : Union [llist .List , ISeq ],
1346
- arities : List [DefTypeMethodArityBase ],
1344
+ arities : List [DefTypeMethodArity ],
1347
1345
special_form : sym .Symbol ,
1348
- ) -> DefTypeMethodBase :
1346
+ ) -> DefTypeMember :
1349
1347
"""Roll all of the collected `deftype*` or `reify*` arities up into a single
1350
1348
method node."""
1351
1349
assert special_form in {SpecialForm .DEFTYPE , SpecialForm .REIFY }
@@ -1354,13 +1352,6 @@ def __deftype_or_reify_method_node_from_arities( # pylint: disable=too-many-bra
1354
1352
fixed_arity_for_variadic : Optional [int ] = None
1355
1353
num_variadic = 0
1356
1354
for arity in arities :
1357
- if fixed_arity_for_variadic is not None :
1358
- if arity .fixed_arity >= fixed_arity_for_variadic :
1359
- raise AnalyzerException (
1360
- f"{ special_form } method may not have a method with fixed arity "
1361
- "greater than fixed arity of variadic function" ,
1362
- form = arity .form ,
1363
- )
1364
1355
if arity .is_variadic :
1365
1356
if num_variadic > 0 :
1366
1357
raise AnalyzerException (
@@ -1397,41 +1388,14 @@ def __deftype_or_reify_method_node_from_arities( # pylint: disable=too-many-bra
1397
1388
form = form ,
1398
1389
)
1399
1390
1400
- max_fixed_arity = max (arity .fixed_arity for arity in arities )
1401
-
1402
- if all (isinstance (e , DefTypeMethodArity ) for e in arities ):
1403
- return DefTypeMethod (
1404
- form = form ,
1405
- name = arities [0 ].name ,
1406
- max_fixed_arity = max_fixed_arity ,
1407
- arities = vec .vector (arities ), # type: ignore[arg-type]
1408
- is_variadic = num_variadic == 1 ,
1409
- env = ctx .get_node_env (),
1410
- )
1411
- elif all (isinstance (e , DefTypeClassMethodArity ) for e in arities ):
1412
- return DefTypeClassMethod (
1413
- form = form ,
1414
- name = arities [0 ].name ,
1415
- max_fixed_arity = max_fixed_arity ,
1416
- arities = vec .vector (arities ), # type: ignore[arg-type]
1417
- is_variadic = num_variadic == 1 ,
1418
- env = ctx .get_node_env (),
1419
- )
1420
- elif all (isinstance (e , DefTypeStaticMethodArity ) for e in arities ):
1421
- return DefTypeStaticMethod (
1422
- form = form ,
1423
- name = arities [0 ].name ,
1424
- max_fixed_arity = max_fixed_arity ,
1425
- arities = vec .vector (arities ), # type: ignore[arg-type]
1426
- is_variadic = num_variadic == 1 ,
1427
- env = ctx .get_node_env (),
1428
- )
1429
- else :
1430
- raise AnalyzerException (
1431
- "deftype* method arities must all be declared one of :classmethod, "
1432
- ":property, :staticmethod, or none (for a standard method)" ,
1433
- form = form ,
1434
- )
1391
+ return DefTypeMethod (
1392
+ form = form ,
1393
+ name = arities [0 ].name ,
1394
+ max_fixed_arity = max (arity .fixed_arity for arity in arities ),
1395
+ arities = vec .vector (arities ),
1396
+ is_variadic = num_variadic == 1 ,
1397
+ env = ctx .get_node_env (),
1398
+ )
1435
1399
1436
1400
1437
1401
def __deftype_or_reify_impls ( # pylint: disable=too-many-branches,too-many-locals # noqa: MC0001
@@ -1484,10 +1448,10 @@ def __deftype_or_reify_impls( # pylint: disable=too-many-branches,too-many-loca
1484
1448
# keys to act as an ordered set of members we've seen. We don't want to register
1485
1449
# duplicates.
1486
1450
member_order = {}
1487
- methods : MutableMapping [
1488
- str , List [ DefTypeMethodArityBase ]
1489
- ] = collections . defaultdict ( list )
1490
- props : MutableMapping [str , DefTypeProperty ] = {}
1451
+ methods : MutableMapping [str , List [ DefTypeMethodArity ]] = collections . defaultdict (
1452
+ list
1453
+ )
1454
+ py_members : MutableMapping [str , DefTypePythonMember ] = {}
1491
1455
for elem in runtime .nthrest (form , 2 ):
1492
1456
if not isinstance (elem , ISeq ):
1493
1457
raise AnalyzerException (
@@ -1497,24 +1461,29 @@ def __deftype_or_reify_impls( # pylint: disable=too-many-branches,too-many-loca
1497
1461
1498
1462
member = __deftype_or_reify_prop_or_method_arity (ctx , elem , special_form )
1499
1463
member_order [member .name ] = True
1500
- if isinstance (member , DefTypeProperty ):
1501
- if member .name in props :
1464
+ if isinstance (
1465
+ member , (DefTypeClassMethod , DefTypeProperty , DefTypeStaticMethod )
1466
+ ):
1467
+ if member .name in py_members :
1502
1468
raise AnalyzerException (
1503
- f"{ special_form } property may only have one arity defined" ,
1469
+ f"{ special_form } class methods, properties, and static methods "
1470
+ "may only have one arity defined" ,
1504
1471
form = elem ,
1505
1472
lisp_ast = member ,
1506
1473
)
1507
1474
elif member .name in methods :
1508
1475
raise AnalyzerException (
1509
- f"{ special_form } property name already defined as a method" ,
1476
+ f"{ special_form } class method, property, or static method name "
1477
+ "already defined as a method" ,
1510
1478
form = elem ,
1511
1479
lisp_ast = member ,
1512
1480
)
1513
- props [member .name ] = member
1481
+ py_members [member .name ] = member
1514
1482
else :
1515
- if member .name in props :
1483
+ if member .name in py_members :
1516
1484
raise AnalyzerException (
1517
- f"{ special_form } method name already defined as a property" ,
1485
+ f"{ special_form } method name already defined as a class method, "
1486
+ "property, or static method" ,
1518
1487
form = elem ,
1519
1488
lisp_ast = member ,
1520
1489
)
@@ -1531,9 +1500,9 @@ def __deftype_or_reify_impls( # pylint: disable=too-many-branches,too-many-loca
1531
1500
)
1532
1501
continue
1533
1502
1534
- prop = props .get (member_name )
1535
- assert prop is not None , "Member must be a method or property"
1536
- members .append (prop )
1503
+ py_member = py_members .get (member_name )
1504
+ assert py_member is not None , "Member must be a method or property"
1505
+ members .append (py_member )
1537
1506
1538
1507
return interfaces , members
1539
1508
@@ -1557,7 +1526,7 @@ def __deftype_and_reify_impls_are_all_abstract( # pylint: disable=too-many-bran
1557
1526
assert special_form in {SpecialForm .DEFTYPE , SpecialForm .REIFY }
1558
1527
1559
1528
field_names = frozenset (fields )
1560
- member_names = frozenset (munge ( member . name ) for member in members )
1529
+ member_names = frozenset (deftype_or_reify_python_member_names ( members ) )
1561
1530
all_member_names = field_names .union (member_names )
1562
1531
all_interface_methods : Set [str ] = set ()
1563
1532
for interface in interfaces :
@@ -1916,13 +1885,6 @@ def _fn_ast( # pylint: disable=too-many-branches
1916
1885
fixed_arity_for_variadic : Optional [int ] = None
1917
1886
num_variadic = 0
1918
1887
for arity in arities :
1919
- if fixed_arity_for_variadic is not None :
1920
- if arity .fixed_arity >= fixed_arity_for_variadic :
1921
- raise AnalyzerException (
1922
- "fn may not have a method with fixed arity greater than "
1923
- "fixed arity of variadic function" ,
1924
- form = arity .form ,
1925
- )
1926
1888
if arity .is_variadic :
1927
1889
if num_variadic > 0 :
1928
1890
raise AnalyzerException (
0 commit comments