@@ -315,3 +315,36 @@ def __init__(self, r: float): ...
315315 point_b = module ["PointB" ]
316316 assert ["self" , "r" ] == [p .name for p in point_b .parameters ]
317317 assert ["x" , "y" , "z" , "__init__" ] == list (point_b .members )
318+
319+
320+ def test_module_source () -> None :
321+ """Check the source property of a module."""
322+ code = "print('hello')\n print('world')"
323+ with temporary_visited_package ("package" , {"__init__.py" : code }) as module :
324+ assert module .source == code
325+
326+
327+ def test_class_source () -> None :
328+ """Check the source property of a class."""
329+ code = """
330+ class A:
331+ def __init__(self, x: int):
332+ self.x = x
333+ """
334+ with temporary_visited_package ("package" , {"__init__.py" : code }) as module :
335+ assert module ["A" ].source == dedent (code ).strip ()
336+
337+
338+ def test_object_source_with_missing_line_number () -> None :
339+ """Check the source property of an object with missing line number."""
340+ code = """
341+ class A:
342+ def __init__(self, x: int):
343+ self.x = x
344+ """
345+ with temporary_visited_package ("package" , {"__init__.py" : code }) as module :
346+ module ["A" ].endlineno = None
347+ assert not module ["A" ].source
348+ module ["A" ].endlineno = 3
349+ module ["A" ].lineno = None
350+ assert not module ["A" ].source
0 commit comments