Skip to content

Commit 8702f62

Browse files
committed
REF: Remove "ForwardRef" in evaluated annotations
When the stringified object is not available for literal replacement, `ForwardRef('...')` is used, but by that time we already need the whole annotation, so just unwrap it. Longer-term should maybe switch away from using `inspect.formatannotation()`.
1 parent cb74938 commit 8702f62

File tree

4 files changed

+24
-9
lines changed

4 files changed

+24
-9
lines changed

pdoc/__init__.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -1261,7 +1261,12 @@ def return_annotation(self, *, link=None) -> str:
12611261
if annot is inspect.Parameter.empty or not annot:
12621262
return ''
12631263

1264-
s = annot if isinstance(annot, str) else inspect.formatannotation(annot)
1264+
if isinstance(annot, str):
1265+
s = annot
1266+
else:
1267+
s = inspect.formatannotation(annot)
1268+
s = re.sub(r'\b(typing\.)?ForwardRef\((?P<quot>[\"\'])(?P<str>.*?)(?P=quot)\)',
1269+
r'\g<str>', s)
12651270
s = s.replace(' ', '\N{NBSP}') # Better line breaks in html signatures
12661271

12671272
if link:

pdoc/test/__init__.py

+13-8
Original file line numberDiff line numberDiff line change
@@ -414,14 +414,19 @@ def test_skip_errors(self):
414414
@unittest.skipIf(sys.version_info < (3, 6), 'variable annotation unsupported in <Py3.6')
415415
def test_resolve_typing_forwardrefs(self):
416416
# GH-245
417-
with chdir(os.path.join(TESTS_BASEDIR, EXAMPLE_MODULE, '_resolve_typing_forwardrefs')),\
418-
redirect_streams() as (out, err):
419-
run('foo')
420-
out = out.getvalue()
421-
self.assertIn('bar', out)
422-
self.assertIn('baz', out)
423-
self.assertIn('dt', out)
424-
self.assertIn('datetime', out)
417+
with chdir(os.path.join(TESTS_BASEDIR, EXAMPLE_MODULE, '_resolve_typing_forwardrefs')):
418+
with redirect_streams() as (out, _err):
419+
run('postponed')
420+
out = out.getvalue()
421+
self.assertIn('bar', out)
422+
self.assertIn('baz', out)
423+
self.assertIn('dt', out)
424+
self.assertIn('datetime', out)
425+
426+
with redirect_streams() as (out, _err):
427+
run('evaluated')
428+
out = out.getvalue()
429+
self.assertIn('Set[Bar]', out)
425430

426431

427432
class ApiTest(unittest.TestCase):
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from typing import Set
2+
3+
4+
class Foo:
5+
bar: Set["Bar"] # noqa: F821

0 commit comments

Comments
 (0)