Skip to content

Commit

Permalink
feat: Stubs are created for referenced declarations in other packages (
Browse files Browse the repository at this point in the history
…#70)

Closes #66

### Summary of Changes

Now limited stubs are created for imported classes from other packages.

---------

Co-authored-by: megalinter-bot <[email protected]>
  • Loading branch information
Masara and megalinter-bot authored Mar 3, 2024
1 parent 78df1cd commit 522f38d
Show file tree
Hide file tree
Showing 28 changed files with 488 additions and 220 deletions.
367 changes: 225 additions & 142 deletions src/safeds_stubgen/stubs_generator/_generate_stubs.py

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions tests/data/main_package/another_path/another_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,7 @@

class AnotherClass:
pass


class yetAnotherClass:
def another_function(self) -> str: ...
5 changes: 4 additions & 1 deletion tests/data/various_modules_package/class_module.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from tests.data.main_package.another_path.another_module import yetAnotherClass


class ClassModuleEmptyClassA:
...

Expand All @@ -8,7 +11,7 @@ def __init__(self, a: int, b: ClassModuleEmptyClassA | None): ...
def f(self): ...


class ClassModuleClassC(ClassModuleEmptyClassA, ClassModuleClassB):
class ClassModuleClassC(ClassModuleEmptyClassA, ClassModuleClassB, yetAnotherClass):
attr_1: int
attr_2: int

Expand Down
70 changes: 70 additions & 0 deletions tests/safeds_stubgen/__snapshots__/test_main.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,29 @@
'type_parameters': list([
]),
}),
dict({
'attributes': list([
]),
'classes': list([
]),
'constructor': None,
'docstring': dict({
'description': '',
'full_docstring': '',
}),
'id': 'main_package/another_path/another_module/yetAnotherClass',
'is_public': True,
'methods': list([
'main_package/another_path/another_module/yetAnotherClass/another_function',
]),
'name': 'yetAnotherClass',
'reexported_by': list([
]),
'superclasses': list([
]),
'type_parameters': list([
]),
}),
dict({
'attributes': list([
'main_package/main_module/ModuleClass/attr_1',
Expand Down Expand Up @@ -307,6 +330,26 @@
'enums': list([
]),
'functions': list([
dict({
'docstring': dict({
'description': '',
'full_docstring': '',
}),
'id': 'main_package/another_path/another_module/yetAnotherClass/another_function',
'is_class_method': False,
'is_property': False,
'is_public': True,
'is_static': False,
'name': 'another_function',
'parameters': list([
'main_package/another_path/another_module/yetAnotherClass/another_function/self',
]),
'reexported_by': list([
]),
'results': list([
'main_package/another_path/another_module/yetAnotherClass/another_function/result_1',
]),
}),
dict({
'docstring': dict({
'description': '',
Expand Down Expand Up @@ -495,6 +538,7 @@
dict({
'classes': list([
'main_package/another_path/another_module/AnotherClass',
'main_package/another_path/another_module/yetAnotherClass',
]),
'docstring': '''
Another Module Docstring.
Expand Down Expand Up @@ -554,6 +598,19 @@
]),
'package': 'main_package',
'parameters': list([
dict({
'assigned_by': 'IMPLICIT',
'default_value': None,
'docstring': dict({
'default_value': '',
'description': '',
'type': '',
}),
'id': 'main_package/another_path/another_module/yetAnotherClass/another_function/self',
'is_optional': False,
'name': 'self',
'type': None,
}),
dict({
'assigned_by': 'POSITION_OR_NAME',
'default_value': None,
Expand Down Expand Up @@ -729,6 +786,19 @@
}),
]),
'results': list([
dict({
'docstring': dict({
'description': '',
'type': '',
}),
'id': 'main_package/another_path/another_module/yetAnotherClass/another_function/result_1',
'name': 'result_1',
'type': dict({
'kind': 'NamedType',
'name': 'str',
'qname': 'builtins.str',
}),
}),
dict({
'docstring': dict({
'description': '',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2035,6 +2035,7 @@
'superclasses': list([
'tests.data.various_modules_package.class_module.ClassModuleEmptyClassA',
'tests.data.various_modules_package.class_module.ClassModuleClassB',
'tests.data.main_package.another_path.another_module.yetAnotherClass',
]),
'type_parameters': list([
]),
Expand Down Expand Up @@ -6431,6 +6432,10 @@
'id': 'various_modules_package/class_module',
'name': 'class_module',
'qualified_imports': list([
dict({
'alias': None,
'qualified_name': 'tests.data.main_package.another_path.another_module.yetAnotherClass',
}),
]),
'wildcard_imports': list([
]),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@PythonModule("various_modules_package.file_creation._module_3")
package variousModulesPackage.fileCreation.Module3

class Reexported()
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@PythonModule("various_modules_package.file_creation._module_6")
package variousModulesPackage.fileCreation.Module6

// TODO Result type information missing.
@Pure
@PythonName("public_reexported")
fun publicReexported()
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@PythonModule("various_modules_package._reexport_module_1")
package variousModulesPackage.ReexportModule1

class ReexportClass()
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
@PythonModule("various_modules_package._reexport_module_2")
package variousModulesPackage.ReexportModule2

// TODO Result type information missing.
@Pure
@PythonName("reexported_function_2")
fun reexportedFunction2()

class AnotherReexportClass()
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@PythonModule("various_modules_package._reexport_module_4")
package variousModulesPackage.ReexportModule4

class FourthReexportClass()
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@PythonModule("various_modules_package.another_path.another_module")
package variousModulesPackage.anotherPath.anotherModule

class AnotherClass()
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
@PythonModule("various_modules_package.attribute_module")
package variousModulesPackage.attributeModule

from tests.data.mainPackage.anotherPath.anotherModule import AnotherClass

class AttributesClassA()

class AttributesClassB() {
Expand Down Expand Up @@ -92,7 +94,7 @@ class AttributesClassB() {
@PythonName("attr_type_from_outside_package")
static attr attrTypeFromOutsidePackage: AnotherClass
@PythonName("attr_default_value_from_outside_package")
static attr attrDefaultValueFromOutsidePackage: () -> a: AnotherClass
static attr attrDefaultValueFromOutsidePackage: () -> param1: AnotherClass
@PythonName("init_attr")
attr initAttr: Boolean

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
@PythonModule("various_modules_package.class_module")
package variousModulesPackage.classModule

from tests.data.mainPackage.anotherPath.anotherModule import yetAnotherClass

class ClassModuleEmptyClassA()

class ClassModuleClassB(
Expand All @@ -13,7 +15,7 @@ class ClassModuleClassB(
}

// TODO Safe-DS does not support multiple inheritance.
class ClassModuleClassC() sub ClassModuleEmptyClassA, ClassModuleClassB {
class ClassModuleClassC() sub ClassModuleEmptyClassA, ClassModuleClassB, yetAnotherClass {
@PythonName("attr_1")
static attr attr1: Int
@PythonName("attr_2")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
@PythonModule("various_modules_package.docstring_module")
package variousModulesPackage.docstringModule

class EpydocDocstringClass(
@PythonName("param_1") param1: String
) {
@PythonName("attr_1")
static attr attr1: String

@Pure
@PythonName("epydoc_docstring_func")
fun epydocDocstringFunc(
x: Int,
y: Int
) -> result1: Boolean
}

class RestDocstringClass(
@PythonName("param_1") param1: String
) {
@PythonName("attr_1")
static attr attr1: String

@Pure
@PythonName("rest_docstring_func")
fun restDocstringFunc(
x: Int,
y: Int
) -> result1: Boolean
}

class NumpyDocstringClass(
@PythonName("param_1") param1: String
) {
@PythonName("attr_1")
static attr attr1: String

@Pure
@PythonName("numpy_docstring_func")
fun numpyDocstringFunc(
x: Int,
y: Int
) -> result1: Boolean
}

class GoogleDocstringClass(
@PythonName("param_1") param1: String
) {
@PythonName("attr_1")
static attr attr1: String

@Pure
@PythonName("google_docstring_func")
fun googleDocstringFunc(
x: Int,
y: Int
) -> result1: Boolean
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
@PythonModule("various_modules_package.function_module")
package variousModulesPackage.functionModule

from tests.data.mainPackage.anotherPath.anotherModule import AnotherClass

// TODO Result type information missing.
@Pure
@PythonName("public_no_params_no_result")
Expand Down Expand Up @@ -230,8 +232,8 @@ fun anyResults() -> result1: Any
@Pure
@PythonName("callable_type")
fun callableType(
param: (a: String) -> (b: Int, c: String)
) -> result1: (a: Int, b: Int) -> c: Int
param: (param1: String) -> (param2: Int, param3: String)
) -> result1: (param1: Int, param2: Int) -> param3: Int

// TODO Result type information missing.
// TODO Some parameter have no type information.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class InferMyTypes(
@PythonName("infer_none")
static attr inferNone: Nothing?
@PythonName("infer_obj")
static attr inferObj: () -> a: InferMe
static attr inferObj: () -> param1: InferMe
// TODO Attribute has no type information.
@PythonName("init_infer")
attr initInfer
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@PythonModule("various_modules_package.file_creation.module_1")
package variousModulesPackage.fileCreation.module1

class C()
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@PythonModule("various_modules_package.file_creation.package_1.module_5")
package variousModulesPackage.fileCreation.package1.module5

class C()
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@PythonModule("tests.data.main_package.another_path.another_module")
package tests.data.mainPackage.anotherPath.anotherModule

class AnotherClass

@PythonName("yetAnotherClass")
class YetAnotherClass
Loading

0 comments on commit 522f38d

Please sign in to comment.