File tree 3 files changed +49
-1
lines changed
3 files changed +49
-1
lines changed Original file line number Diff line number Diff line change 27
27
import types
28
28
from inspect import Parameter
29
29
from inspect import Signature
30
+ from inspect import isclass
30
31
from inspect import signature
31
32
from typing import TYPE_CHECKING
32
33
from typing import Any
@@ -469,8 +470,12 @@ def _repr_dataclass(
469
470
if field .type :
470
471
if isinstance (field .type , str ):
471
472
comment .append (f"type: { field .type } " )
472
- else :
473
+ elif isinstance (field .type , ForwardRef ):
474
+ comment .append (f"type: { field .type !r} " )
475
+ elif isclass (field .type ):
473
476
comment .append (f"type: { field .type .__name__ } " )
477
+ else :
478
+ comment .append (f"type: { field .type !r} " )
474
479
if getattr (field , "kw_only" , False ): # python 3.10+
475
480
comment .append ("kw_only" )
476
481
Original file line number Diff line number Diff line change 18
18
19
19
"""Python independent logwrap tests."""
20
20
21
+ from __future__ import annotations
22
+
23
+ import dataclasses
21
24
import functools
22
25
import io
23
26
import logging
@@ -612,6 +615,33 @@ def func():
612
615
)
613
616
# fmt: on
614
617
618
+ def test_025_union_ann_call_arg (self ):
619
+ @dataclasses .dataclass
620
+ class WithUnionAnn :
621
+ a : int | None
622
+
623
+ @logwrap .logwrap
624
+ def func (val : WithUnionAnn ) -> WithUnionAnn :
625
+ return val
626
+
627
+ func (WithUnionAnn (1 ))
628
+ # fmt: off
629
+ self .assertEqual (
630
+ "DEBUG>Calling: \n "
631
+ "func(\n "
632
+ " # POSITIONAL_OR_KEYWORD:\n "
633
+ " val=test_log_wrap.WithUnionAnn(\n "
634
+ " a=1, # type: int | None\n "
635
+ " ), # type: WithUnionAnn\n "
636
+ ")\n "
637
+ "DEBUG>Done: 'func' with result:\n "
638
+ "test_log_wrap.WithUnionAnn(\n "
639
+ " a=1, # type: int | None\n "
640
+ ")\n " ,
641
+ self .stream .getvalue (),
642
+ )
643
+ # fmt: on
644
+
615
645
616
646
# noinspection PyMissingOrEmptyDocstring
617
647
class TestObject (unittest .TestCase ):
Original file line number Diff line number Diff line change @@ -334,6 +334,19 @@ def test_005_deque(self):
334
334
logwrap .pretty_repr (default_deque ),
335
335
)
336
336
337
+ def tests_006_union_ann (self ):
338
+ @dataclasses .dataclass
339
+ class WithUnionAnn :
340
+ a : int | None
341
+
342
+ test_dc = WithUnionAnn (None )
343
+ self .assertEqual (
344
+ "test_repr_utils.WithUnionAnn(\n "
345
+ " a=None, # type: int | None\n "
346
+ ")" ,
347
+ logwrap .pretty_repr (test_dc ),
348
+ )
349
+
337
350
338
351
class TestRich (unittest .TestCase ):
339
352
class Bird :
You can’t perform that action at this time.
0 commit comments