Skip to content

Commit d72482e

Browse files
committed
Expand unsupported default tests, minor tidy
1 parent 8e7b7cd commit d72482e

File tree

3 files changed

+40
-18
lines changed

3 files changed

+40
-18
lines changed

mypyc/ir/func_ir.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -411,17 +411,17 @@ def get_text_signature(fn: FuncIR, *, bound: bool = False) -> str | None:
411411
if the function's signature cannot be represented.
412412
"""
413413
parameters = []
414-
mark_self = fn.class_name is not None and fn.decl.kind != FUNC_STATICMETHOD and not bound
414+
mark_self = (fn.class_name is not None) and (fn.decl.kind != FUNC_STATICMETHOD) and not bound
415+
sig = fn.decl.bound_sig if bound and fn.decl.bound_sig is not None else fn.decl.sig
415416
# Pre-scan for end of positional-only parameters.
416417
# This is needed to handle signatures like 'def foo(self, __x)', where mypy
417418
# currently sees 'self' as being positional-or-keyword and '__x' as positional-only.
418419
pos_only_idx = -1
419-
sig = fn.decl.bound_sig if bound and fn.decl.bound_sig is not None else fn.decl.sig
420420
for idx, arg in enumerate(sig.args):
421421
if arg.pos_only and arg.kind in (ArgKind.ARG_POS, ArgKind.ARG_OPT):
422422
pos_only_idx = idx
423423
for idx, arg in enumerate(sig.args):
424-
if arg.name.startswith("__bitmap") or arg.name == "__mypyc_self__":
424+
if arg.name.startswith(("__bitmap", "__mypyc")):
425425
continue
426426
kind = (
427427
inspect.Parameter.POSITIONAL_ONLY

mypyc/test-data/run-signatures.test

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ for fn in [f1, f2, f3, f4, f5, f6, f7, f8, f9, f10]:
2929
assert getattr(fn, "__doc__") is None
3030

3131
[case testSignaturesValidDefaults]
32+
from typing import Final
33+
A: Final = 1
34+
3235
def default_int(x=1): pass
3336
def default_str(x="a"): pass
3437
def default_float(x=1.0): pass
@@ -38,6 +41,7 @@ def default_none(x=None): pass
3841
def default_tuple_empty(x=()): pass
3942
def default_tuple_literals(x=(1, "a", 1.0, False, True, None, (), (1,2,(3,4)))): pass
4043
def default_tuple_singleton(x=(1,)): pass
44+
def default_named_constant(x=A): pass
4145

4246
[file driver.py]
4347
import inspect
@@ -51,6 +55,7 @@ assert str(inspect.signature(default_false)) == "(x=False)"
5155
assert str(inspect.signature(default_none)) == "(x=None)"
5256
assert str(inspect.signature(default_tuple_empty)) == "(x=())"
5357
assert str(inspect.signature(default_tuple_literals)) == "(x=(1, 'a', 1.0, False, True, None, (), (1, 2, (3, 4))))"
58+
assert str(inspect.signature(default_named_constant)) == "(x=1)"
5459

5560
# Check __text_signature__ directly since inspect.signature produces
5661
# an incorrect signature for 1-tuple default arguments prior to
@@ -76,25 +81,42 @@ assert str(inspect.signature(f4)) == r"""(x='\\ \x07 \x08 \x0c \n \r \t \x0b \x0
7681
assert str(inspect.signature(f5)) == """(x='\N{BANANA}sv')"""
7782

7883
[case testSignaturesIrrepresentableDefaults]
79-
def bad1(x=[]): pass
80-
def bad2(x={}): pass
81-
def bad3(x=set()): pass
82-
def bad4(x=int): pass
83-
def bad5(x=lambda: None): pass
84-
def bad6(x=bad1): pass
85-
# note: inspect supports constant folding for defaults in text signatures
86-
def bad7(x=1+2): pass
87-
def bad8(x=1-2): pass
88-
def bad9(x=1|2): pass
89-
def bad10(x=float("nan")): pass
90-
def bad11(x=([],)): pass
84+
import enum
85+
class Color(enum.Enum):
86+
RED = 1
87+
misc = object()
88+
89+
# Default arguments that cannot be represented in a __text_signature__
90+
def bad_object(x=misc): pass
91+
def bad_list_nonliteral(x=[misc]): pass
92+
def bad_dict_nonliteral(x={'a': misc}): pass
93+
def bad_set_nonliteral(x={misc}): pass
94+
def bad_set_empty(x=set()): pass # supported by ast.literal_eval, but not by inspect._signature_fromstr
95+
def bad_nan(x=float("nan")): pass
96+
def bad_enum(x=Color.RED): pass
97+
98+
# TODO: Default arguments that could potentially be represented in a
99+
# __text_signature__, but which are not currently supported.
100+
# See 'inspect._signature_fromstr' for what default values are supported at runtime.
101+
def bad_complex(x=1+2j): pass
102+
def bad_list_empty(x=[]): pass
103+
def bad_list_literals(x=[1, 2, 3]): pass
104+
def bad_dict_empty(x={}): pass
105+
def bad_dict_literals(x={'a': 1}): pass
106+
def bad_set_literals(x={1, 2, 3}): pass
107+
def bad_tuple_literals(x=([1, 2, 3], {'a': 1}, {1, 2, 3})): pass
108+
def bad_ellipsis(x=...): pass
109+
def bad_literal_fold(x=1+2): pass
91110

92111
[file driver.py]
93112
import inspect
94113
from testutil import assertRaises
95-
from native import *
114+
import native
115+
116+
all_bad = [fn for name, fn in vars(native).items() if name.startswith("bad_")]
117+
assert all_bad
96118

97-
for bad in [bad1, bad2, bad3, bad4, bad5, bad6, bad7, bad8, bad9, bad10, bad11]:
119+
for bad in all_bad:
98120
assert bad.__text_signature__ is None, f"{bad.__name__} has unexpected __text_signature__"
99121
with assertRaises(ValueError, "no signature found for builtin"):
100122
inspect.signature(bad)

mypyc/test/test_run.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,9 @@
6969
"run-dunders-special.test",
7070
"run-singledispatch.test",
7171
"run-attrs.test",
72+
"run-signatures.test",
7273
"run-python37.test",
7374
"run-python38.test",
74-
"run-signatures.test",
7575
]
7676

7777
if sys.version_info >= (3, 10):

0 commit comments

Comments
 (0)