Skip to content

Commit

Permalink
feat: Added generation for Safe-DS stubs files (#33)
Browse files Browse the repository at this point in the history
### Summary of Changes
Added the automatic generation of Safe-DS stubs files after an api call.

---------

Co-authored-by: megalinter-bot <129584137+megalinter-bot@users.noreply.github.com>
Co-authored-by: Lars Reimann <mail@larsreimann.com>
  • Loading branch information
3 people authored Dec 7, 2023
1 parent b7f86e3 commit ab45b45
Show file tree
Hide file tree
Showing 59 changed files with 8,330 additions and 5,042 deletions.
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,8 @@ charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.ambr]
trim_trailing_whitespace = false

[{*.yaml,*.yml}]
indent_size = 2
5 changes: 4 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ packages = [
]

[tool.poetry.scripts]
generate-safeds-stubs = "safeds_stubgen.main:main"
safe-ds-stubgen = "safeds_stubgen.main:main"

[tool.poetry.dependencies]
python = "^3.11"
Expand All @@ -34,5 +34,8 @@ mkdocs-material = "^9.4.7"
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"

[tool.pytest.ini_options]
addopts = "--ignore=./tests/data"

[tool.black]
line-length = 120
26 changes: 23 additions & 3 deletions src/safeds_stubgen/api_analyzer/__init__.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,34 @@
"""API-Analyzer for the Safe-DS stubs generator."""

from __future__ import annotations

from ._api import API, Attribute, Class, Function, Parameter, ParameterAssignment
from ._api import (
API,
Attribute,
Class,
Enum,
Function,
Module,
Parameter,
ParameterAssignment,
QualifiedImport,
Result,
VarianceKind,
WildcardImport,
)
from ._get_api import get_api
from ._mypy_helpers import get_classdef_definitions, get_funcdef_definitions, get_mypyfile_definitions
from ._package_metadata import distribution, distribution_version, package_root
from ._types import (
AbstractType,
BoundaryType,
CallableType,
DictType,
EnumType,
FinalType,
ListType,
LiteralType,
NamedType,
OptionalType,
SetType,
TupleType,
UnionType,
Expand All @@ -25,10 +39,12 @@
"API",
"Attribute",
"BoundaryType",
"CallableType",
"Class",
"DictType",
"distribution",
"distribution_version",
"Enum",
"EnumType",
"FinalType",
"Function",
Expand All @@ -38,12 +54,16 @@
"get_mypyfile_definitions",
"ListType",
"LiteralType",
"Module",
"NamedType",
"OptionalType",
"package_root",
"Parameter",
"ParameterAssignment",
"QualifiedImport",
"Result",
"SetType",
"TupleType",
"UnionType",
"VarianceKind",
"WildcardImport",
]
32 changes: 29 additions & 3 deletions src/safeds_stubgen/api_analyzer/_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ def to_dict(self) -> dict[str, Any]:
class Class:
id: str
name: str
superclasses: list[Class]
superclasses: list[str]
is_public: bool
docstring: ClassDocstring
constructor: Function | None = None
Expand All @@ -174,6 +174,7 @@ class Class:
attributes: list[Attribute] = field(default_factory=list)
methods: list[Function] = field(default_factory=list)
classes: list[Class] = field(default_factory=list)
type_parameters: list[TypeParameter] = field(default_factory=list)

def to_dict(self) -> dict[str, Any]:
return {
Expand All @@ -187,8 +188,13 @@ def to_dict(self) -> dict[str, Any]:
"attributes": [attribute.id for attribute in self.attributes],
"methods": [method.id for method in self.methods],
"classes": [class_.id for class_ in self.classes],
"type_parameters": [type_parameter.to_dict() for type_parameter in self.type_parameters],
}

@property
def is_abstract(self) -> bool:
return "abc.ABC" in self.superclasses

def add_method(self, method: Function) -> None:
self.methods.append(method)

Expand Down Expand Up @@ -229,6 +235,8 @@ class Function:
docstring: FunctionDocstring
is_public: bool
is_static: bool
is_class_method: bool
is_property: bool
results: list[Result] = field(default_factory=list)
reexported_by: list[Module] = field(default_factory=list)
parameters: list[Parameter] = field(default_factory=list)
Expand All @@ -240,6 +248,8 @@ def to_dict(self) -> dict[str, Any]:
"docstring": self.docstring.to_dict(),
"is_public": self.is_public,
"is_static": self.is_static,
"is_class_method": self.is_class_method,
"is_property": self.is_property,
"results": [result.id for result in self.results],
"reexported_by": [module.id for module in self.reexported_by],
"parameters": [parameter.id for parameter in self.parameters],
Expand All @@ -254,7 +264,7 @@ class Parameter:
default_value: str | bool | int | float | None
assigned_by: ParameterAssignment
docstring: ParameterDocstring
type: AbstractType
type: AbstractType | None

@property
def is_required(self) -> bool:
Expand All @@ -275,7 +285,7 @@ def to_dict(self) -> dict[str, Any]:
"is_optional": self.is_optional,
"default_value": self.default_value,
"assigned_by": self.assigned_by.name,
"type": self.type.to_dict(),
"type": self.type.to_dict() if self.type is not None else None,
}


Expand All @@ -297,6 +307,22 @@ class ParameterAssignment(PythonEnum):
NAMED_VARARG = "NAMED_VARARG"


@dataclass(frozen=True)
class TypeParameter:
name: str
type: AbstractType
variance: VarianceKind

def to_dict(self) -> dict[str, Any]:
return {"name": self.name, "type": self.type.to_dict(), "variance_type": self.variance.name}


class VarianceKind(PythonEnum):
CONTRAVARIANT = "CONTRAVARIANT"
COVARIANT = "COVARIANT"
INVARIANT = "INVARIANT"


@dataclass(frozen=True)
class Result:
id: str
Expand Down
Loading

0 comments on commit ab45b45

Please sign in to comment.