Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[python] always generate models with base init #5517

Merged
merged 15 commits into from
Jan 9, 2025
6 changes: 6 additions & 0 deletions packages/http-client-python/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Change Log - @typespec/http-client-python

## 0.5.2

### Bug Fixes

- Generate `__init__` for all DPG models to ensure stability

## 0.5.1

### Bug Fixes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,5 @@ def instance_check_template(self) -> str:

def imports(self, **kwargs: Any) -> FileImport:
file_import = super().imports(**kwargs)
if self.flattened_property:
file_import.add_submodule_import("typing", "Any", ImportType.STDLIB)
file_import.add_submodule_import("typing", "Any", ImportType.STDLIB)
return file_import
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def properties_to_pass_to_super(model: ModelType) -> str:
@abstractmethod
def initialize_properties(self, model: ModelType) -> List[str]: ...

def need_init(self, model: ModelType) -> bool:
def init_overloads(self, model: ModelType) -> bool:
return (not model.internal) and bool(self.init_line(model) or model.discriminator)

def pylint_disable_items(self, model: ModelType) -> List[str]:
Expand All @@ -116,7 +116,7 @@ def pylint_disable_items(self, model: ModelType) -> List[str]:
if any(p for p in model.properties if p.is_discriminator and model.discriminator_value):
return [""]
if model.parents and any(
"=" in prop for parent in model.parents for prop in self.init_line(parent) if self.need_init(parent)
"=" in prop for parent in model.parents for prop in self.init_line(parent) if self.init_overloads(parent)
):
return [""]
return ["useless-super-delegation"]
Expand Down Expand Up @@ -325,7 +325,7 @@ def properties_to_pass_to_super(model: ModelType) -> str:
def global_pylint_disables(self) -> str:
result = []
for model in self.code_model.model_types:
if self.need_init(model):
if self.init_overloads(model):
for item in self.pylint_disable_items(model):
if item:
result.append(item)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
{% endif %}


{% if serializer.need_init(model) %}
{% if serializer.init_overloads(model) %}
iscai-msft marked this conversation as resolved.
Show resolved Hide resolved
@overload
def __init__({{ model.init_pylint_disable }}
self,
Expand All @@ -69,7 +69,6 @@

{% endif %}
{% set initialize_properties = serializer.initialize_properties(model) %}
{% if serializer.need_init(model) or initialize_properties %}
def __init__(self, *args: Any, **kwargs: Any) -> None:
{% for line in serializer.super_call(model) %}
{{ line }}
Expand All @@ -94,4 +93,3 @@
else:
super().__setattr__(key, value)
{% endif %}
{% endif %}
2 changes: 1 addition & 1 deletion packages/http-client-python/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@typespec/http-client-python",
"version": "0.5.1",
"version": "0.5.2",
"author": "Microsoft Corporation",
"description": "TypeSpec emitter for Python SDKs",
"homepage": "https://typespec.io",
Expand Down
Loading