Skip to content

Commit

Permalink
fixed a bug where type names would not be checked for naming conventi…
Browse files Browse the repository at this point in the history
…on and keywords
  • Loading branch information
Masara committed Aug 26, 2024
1 parent fc46724 commit 2fd3e5d
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 18 deletions.
5 changes: 5 additions & 0 deletions src/safeds_stubgen/stubs_generator/_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,8 @@ def _replace_if_safeds_keyword(keyword: str) -> str:
}:
return f"`{keyword}`"
return keyword


def _name_convention_and_keyword_check(name: str, naming_convention: NamingConvention) -> str:
name = _convert_name_to_convention(name=name, naming_convention=naming_convention)
return _replace_if_safeds_keyword(keyword=name)
24 changes: 8 additions & 16 deletions src/safeds_stubgen/stubs_generator/_stub_string_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
_convert_name_to_convention,
_create_name_annotation,
_get_shortest_public_reexport,
_name_convention_and_keyword_check,
_replace_if_safeds_keyword,
)

Expand Down Expand Up @@ -174,13 +175,8 @@ def _create_imports_string(self) -> str:
import_parts = import_.split(".")

from_ = ".".join(import_parts[0:-1])
from_ = _convert_name_to_convention(from_, self.naming_convention)
from_ = _replace_if_safeds_keyword(from_)

name = import_parts[-1]
name = _convert_name_to_convention(name, self.naming_convention)
name = _replace_if_safeds_keyword(name)

from_ = _name_convention_and_keyword_check(from_, self.naming_convention)
name = _name_convention_and_keyword_check(import_parts[-1], self.naming_convention)
import_strings.append(f"from {from_} import {name}")

# We have to sort for the snapshot tests
Expand Down Expand Up @@ -231,8 +227,7 @@ def _create_class_string(self, class_: Class, class_indentation: str = "", in_re
}[variance.variance.name]

# Convert name to camelCase and check for keywords
variance_name_camel_case = _convert_name_to_convention(variance.name, self.naming_convention)
variance_name_camel_case = _replace_if_safeds_keyword(variance_name_camel_case)
variance_name_camel_case = _name_convention_and_keyword_check(variance.name, self.naming_convention)

variance_item = f"{variance_direction}{variance_name_camel_case}"
if variance.type is not None:
Expand Down Expand Up @@ -465,8 +460,7 @@ def _create_function_string(
if function.type_var_types:
type_var_names = []
for type_var in function.type_var_types:
type_var_name = _convert_name_to_convention(type_var.name, self.naming_convention)
type_var_name = _replace_if_safeds_keyword(type_var_name)
type_var_name = _name_convention_and_keyword_check(type_var.name, self.naming_convention)

# We don't have to display generic types in methods if they were already displayed in the class
if not is_method or (is_method and type_var_name not in self.class_generics):
Expand Down Expand Up @@ -547,8 +541,7 @@ def _create_result_string(self, function_results: list[Result]) -> str:

ret_type = self._create_type_string(result_type)
type_string = f": {ret_type}" if ret_type else ""
result_name = _convert_name_to_convention(result.name, self.naming_convention)
result_name = _replace_if_safeds_keyword(result_name)
result_name = _name_convention_and_keyword_check(result.name, self.naming_convention)
if type_string:
results.append(
f"{result_name}{type_string}",
Expand Down Expand Up @@ -712,7 +705,7 @@ def _create_type_string(self, type_data: dict | None) -> str:
if name[0] == "_" and type_data["qname"] not in self.module_imports:
self._current_todo_msgs.add("internal class as type")

return name
return _name_convention_and_keyword_check(name, self.naming_convention)
elif kind == "FinalType":
return self._create_type_string(type_data["type"])
elif kind == "CallableType":
Expand Down Expand Up @@ -851,8 +844,7 @@ def _create_type_string(self, type_data: dict | None) -> str:
types.append(f"{literal_type}")
return f"literal<{', '.join(types)}>"
elif kind == "TypeVarType":
name = _convert_name_to_convention(type_data["name"], self.naming_convention)
return _replace_if_safeds_keyword(name)
return _name_convention_and_keyword_check(type_data["name"], self.naming_convention)

raise ValueError(f"Unexpected type: {kind}") # pragma: no cover

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class AliasingModuleClassC() {
static attr typedAliasAttr: AliasingModuleClassB
// TODO An internal class must not be used as a type in a public class.
@PythonName("infer_alias_attr")
static attr inferAliasAttr: _AliasingModuleClassA
static attr inferAliasAttr: AliasingModuleClassA
@PythonName("typed_alias_attr2")
static attr typedAliasAttr2: AliasingModule2ClassA
@PythonName("infer_alias_attr2")
Expand All @@ -22,5 +22,5 @@ class AliasingModuleClassC() {
// TODO An internal class must not be used as a type in a public class.
// TODO List type has to many type arguments.
@PythonName("alias_list")
static attr aliasList: List<union<AliasingModuleClassB, _AliasingModuleClassA>, AliasingModule2ClassA, ImportMeAliasingModuleClass>
static attr aliasList: List<union<AliasingModuleClassA, AliasingModuleClassB>, AliasingModule2ClassA, ImportMeAliasingModuleClass>
}

0 comments on commit 2fd3e5d

Please sign in to comment.