@@ -1846,3 +1846,34 @@ def test_known_instance_typevar_resolves_to_class(self):
18461846 assert 'typing' in result ._fully_qualified_name
18471847 finally :
18481848 _cleanup_mapping (mapping , tmpdir , client )
1849+
1850+
1851+ class TestDeclarationDeclaringType :
1852+ """Tests for declaring type on function declarations."""
1853+
1854+ def test_declaration_declaring_type_no_ty_types (self ):
1855+ """Without ty-types, declaring type remains None."""
1856+ source = 'def greet(name: str) -> str:\n return name\n '
1857+ tree = ast .parse (source )
1858+ mapping = PythonTypeMapping (source )
1859+ func_node = tree .body [0 ]
1860+ result = mapping .method_declaration_type (func_node )
1861+ assert result is not None
1862+ assert result ._declaring_type is None
1863+
1864+ @requires_ty_types_cli
1865+ def test_declaration_declaring_type_with_ty_types (self ):
1866+ """With ty-types, a function declaration should get a declaring type from the descriptor."""
1867+ source = 'def greet(name: str) -> str:\n return name\n '
1868+ mapping , tree , tmpdir , client = _make_mapping (source )
1869+ try :
1870+ func_node = tree .body [0 ]
1871+ result = mapping .method_declaration_type (func_node )
1872+ assert result is not None
1873+ assert isinstance (result , JavaType .Method )
1874+ assert result ._declaring_type is not None , \
1875+ "Declaration should have a declaring type, not None"
1876+ assert isinstance (result ._declaring_type , JavaType .Class )
1877+ assert result ._declaring_type ._fully_qualified_name != "<unknown>"
1878+ finally :
1879+ _cleanup_mapping (mapping , tmpdir , client )
0 commit comments