From 7d33c12a5a08a44b4e974029a6c472ad628b8266 Mon Sep 17 00:00:00 2001 From: cribe Date: Tue, 18 Feb 2025 12:50:45 -0500 Subject: [PATCH 1/7] added isinstance checks tox pyright errors --- .../azure/health/deidentification/_model_base.py | 6 ++++-- .../health/deidentification/_serialization.py | 16 +++++++++++++--- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/sdk/healthdataaiservices/azure-health-deidentification/azure/health/deidentification/_model_base.py b/sdk/healthdataaiservices/azure-health-deidentification/azure/health/deidentification/_model_base.py index 43fd8c7e9b1b..5b20378c2ccd 100644 --- a/sdk/healthdataaiservices/azure-health-deidentification/azure/health/deidentification/_model_base.py +++ b/sdk/healthdataaiservices/azure-health-deidentification/azure/health/deidentification/_model_base.py @@ -19,7 +19,7 @@ import email.utils from datetime import datetime, date, time, timedelta, timezone from json import JSONEncoder -from typing_extensions import Self +from typing_extensions import Self, ForwardRef import isodate from azure.core.exceptions import DeserializationError from azure.core import CaseInsensitiveEnumMeta @@ -323,10 +323,12 @@ def _get_type_alias_type(module_name: str, alias_name: str): return types[alias_name] -def _get_model(module_name: str, model_name: str): +def _get_model(module_name: str, model_name: typing.Union[str, ForwardRef]): models = {k: v for k, v in sys.modules[module_name].__dict__.items() if isinstance(v, type)} module_end = module_name.rsplit(".", 1)[0] models.update({k: v for k, v in sys.modules[module_end].__dict__.items() if isinstance(v, type)}) + if isinstance(model_name, ForwardRef): + model_name = model_name.__forward_arg__ if isinstance(model_name, str): model_name = model_name.split(".")[-1] if model_name not in models: diff --git a/sdk/healthdataaiservices/azure-health-deidentification/azure/health/deidentification/_serialization.py b/sdk/healthdataaiservices/azure-health-deidentification/azure/health/deidentification/_serialization.py index 8139854b97bb..59b2b2dc322b 100644 --- a/sdk/healthdataaiservices/azure-health-deidentification/azure/health/deidentification/_serialization.py +++ b/sdk/healthdataaiservices/azure-health-deidentification/azure/health/deidentification/_serialization.py @@ -1585,10 +1585,20 @@ def _instantiate_model(self, response, attrs, additional_properties=None): if callable(response): subtype = getattr(response, "_subtype_map", {}) try: - readonly = [k for k, v in response._validation.items() if v.get("readonly")] - const = [k for k, v in response._validation.items() if v.get("constant")] + if isinstance(response, type): + readonly = [k for k, v in response._validation.items() if v.get("readonly")] + else: + readonly = [] + if isinstance(response, type): + const = [k for k, v in response._validation.items() if v.get("constant")] + else: + const = [] kwargs = {k: v for k, v in attrs.items() if k not in subtype and k not in readonly + const} - response_obj = response(**kwargs) + if isinstance(response, type) and issubclass(response, Model): + response_obj = response(**kwargs) + else: + raise TypeError("Response is not a subclass of Model") + for attr in readonly: setattr(response_obj, attr, attrs.get(attr)) if additional_properties: From 2917b53ed28d15400149ef8203a4d4abb7c52fd6 Mon Sep 17 00:00:00 2001 From: cribe Date: Tue, 18 Feb 2025 13:48:58 -0500 Subject: [PATCH 2/7] Bump version and changelog for pyright fix --- .../azure-health-deidentification/CHANGELOG.md | 6 ++++++ .../azure/health/deidentification/_version.py | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/sdk/healthdataaiservices/azure-health-deidentification/CHANGELOG.md b/sdk/healthdataaiservices/azure-health-deidentification/CHANGELOG.md index be2efaec75db..76b4efaeb6ba 100644 --- a/sdk/healthdataaiservices/azure-health-deidentification/CHANGELOG.md +++ b/sdk/healthdataaiservices/azure-health-deidentification/CHANGELOG.md @@ -1,5 +1,11 @@ # Release History +## 1.0.0b3 (Unreleased) + +### Bugs Fixed + +Fixed tox pyright errors present in the build (Argument of type "Unknown" and Cannot access attribute) + ## 1.0.0b2 (Unreleased) ### Features Added diff --git a/sdk/healthdataaiservices/azure-health-deidentification/azure/health/deidentification/_version.py b/sdk/healthdataaiservices/azure-health-deidentification/azure/health/deidentification/_version.py index bbcd28b4aa67..c43fdbc2e239 100644 --- a/sdk/healthdataaiservices/azure-health-deidentification/azure/health/deidentification/_version.py +++ b/sdk/healthdataaiservices/azure-health-deidentification/azure/health/deidentification/_version.py @@ -6,4 +6,4 @@ # Changes may cause incorrect behavior and will be lost if the code is regenerated. # -------------------------------------------------------------------------- -VERSION = "1.0.0b2" +VERSION = "1.0.0b3" From 4091229653dcd85b0471a17117b5851c20da8cba Mon Sep 17 00:00:00 2001 From: cribe Date: Tue, 18 Feb 2025 16:18:45 -0500 Subject: [PATCH 3/7] Making sure testenv uses typing_extensions 4.12.0 --- eng/tox/tox.ini | 1 + 1 file changed, 1 insertion(+) diff --git a/eng/tox/tox.ini b/eng/tox/tox.ini index 8cb32df12480..5cd110d40445 100644 --- a/eng/tox/tox.ini +++ b/eng/tox/tox.ini @@ -67,6 +67,7 @@ setenv = VIRTUALENV_PIP=24.0 VIRTUALENV_SETUPTOOLS=67.6.0 deps = {[base]deps} + typing_extensions>=4.12.0 install_command = python -m pip install {opts} {packages} --cache-dir {tox_root}/../.tox_pip_cache_{envname} commands = python -m pip --version From 5c8959f647ecc5f7cc5fd752fa06d70ede692cfb Mon Sep 17 00:00:00 2001 From: cribe Date: Wed, 19 Feb 2025 09:39:18 -0500 Subject: [PATCH 4/7] Revert "Making sure testenv uses typing_extensions 4.12.0" This reverts commit 4091229653dcd85b0471a17117b5851c20da8cba. --- eng/tox/tox.ini | 1 - 1 file changed, 1 deletion(-) diff --git a/eng/tox/tox.ini b/eng/tox/tox.ini index 5cd110d40445..8cb32df12480 100644 --- a/eng/tox/tox.ini +++ b/eng/tox/tox.ini @@ -67,7 +67,6 @@ setenv = VIRTUALENV_PIP=24.0 VIRTUALENV_SETUPTOOLS=67.6.0 deps = {[base]deps} - typing_extensions>=4.12.0 install_command = python -m pip install {opts} {packages} --cache-dir {tox_root}/../.tox_pip_cache_{envname} commands = python -m pip --version From 8dd84b19190d2d165d7e244983a318b1f0441720 Mon Sep 17 00:00:00 2001 From: cribe Date: Wed, 19 Feb 2025 10:25:04 -0500 Subject: [PATCH 5/7] ForwardRef fix via getattr --- .../health/deidentification/_model_base.py | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/sdk/healthdataaiservices/azure-health-deidentification/azure/health/deidentification/_model_base.py b/sdk/healthdataaiservices/azure-health-deidentification/azure/health/deidentification/_model_base.py index 5b20378c2ccd..3a8135fdd925 100644 --- a/sdk/healthdataaiservices/azure-health-deidentification/azure/health/deidentification/_model_base.py +++ b/sdk/healthdataaiservices/azure-health-deidentification/azure/health/deidentification/_model_base.py @@ -19,7 +19,7 @@ import email.utils from datetime import datetime, date, time, timedelta, timezone from json import JSONEncoder -from typing_extensions import Self, ForwardRef +from typing_extensions import Self import isodate from azure.core.exceptions import DeserializationError from azure.core import CaseInsensitiveEnumMeta @@ -323,17 +323,16 @@ def _get_type_alias_type(module_name: str, alias_name: str): return types[alias_name] -def _get_model(module_name: str, model_name: typing.Union[str, ForwardRef]): +def _get_model(module_name: str, model_name: typing.Union[str, typing.ForwardRef]) -> typing.Any: models = {k: v for k, v in sys.modules[module_name].__dict__.items() if isinstance(v, type)} module_end = module_name.rsplit(".", 1)[0] models.update({k: v for k, v in sys.modules[module_end].__dict__.items() if isinstance(v, type)}) - if isinstance(model_name, ForwardRef): - model_name = model_name.__forward_arg__ - if isinstance(model_name, str): - model_name = model_name.split(".")[-1] - if model_name not in models: - return model_name - return models[model_name] + name_str = getattr(model_name, "__forward_arg__", model_name) if model_name else "" + if isinstance(name_str, str): + name_str = name_str.split(".")[-1] + if name_str not in models: + return name_str + return models[name_str] _UNSET = object() @@ -434,7 +433,7 @@ def _serialize(o, format: typing.Optional[str] = None): # pylint: disable=too-m if isinstance(o, dict): return {k: _serialize(v, format) for k, v in o.items()} if isinstance(o, set): - return {_serialize(x, format) for x in o} + return list(_serialize(x, format) for x in o) # Convert to list to avoid hashability issues if isinstance(o, tuple): return tuple(_serialize(x, format) for x in o) if isinstance(o, (bytes, bytearray)): From 8d05d829399d230aba4d72840042d7b3292fc424 Mon Sep 17 00:00:00 2001 From: cribe Date: Wed, 19 Feb 2025 11:19:21 -0500 Subject: [PATCH 6/7] fix mypy errors --- .../azure/health/deidentification/_model_base.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/sdk/healthdataaiservices/azure-health-deidentification/azure/health/deidentification/_model_base.py b/sdk/healthdataaiservices/azure-health-deidentification/azure/health/deidentification/_model_base.py index 3a8135fdd925..3764c93668ba 100644 --- a/sdk/healthdataaiservices/azure-health-deidentification/azure/health/deidentification/_model_base.py +++ b/sdk/healthdataaiservices/azure-health-deidentification/azure/health/deidentification/_model_base.py @@ -327,9 +327,20 @@ def _get_model(module_name: str, model_name: typing.Union[str, typing.ForwardRef models = {k: v for k, v in sys.modules[module_name].__dict__.items() if isinstance(v, type)} module_end = module_name.rsplit(".", 1)[0] models.update({k: v for k, v in sys.modules[module_end].__dict__.items() if isinstance(v, type)}) - name_str = getattr(model_name, "__forward_arg__", model_name) if model_name else "" + + # Get the actual name string from the ForwardRef if that's what we received + if isinstance(model_name, typing.ForwardRef): + name_str = typing.cast(str, getattr(model_name, "__forward_arg__", "")) + else: + name_str = typing.cast(str, model_name) if model_name else "" + if isinstance(name_str, str): name_str = name_str.split(".")[-1] + + # Look up the model class by name + if not isinstance(name_str, str): + raise ValueError("Model name must be a string or ForwardRef") + if name_str not in models: return name_str return models[name_str] From 3f4241baa3e0e26b378a285def231e120ca8e649 Mon Sep 17 00:00:00 2001 From: cribe Date: Wed, 19 Feb 2025 12:07:25 -0500 Subject: [PATCH 7/7] rem trailing whitespaces --- .../azure/health/deidentification/_model_base.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/sdk/healthdataaiservices/azure-health-deidentification/azure/health/deidentification/_model_base.py b/sdk/healthdataaiservices/azure-health-deidentification/azure/health/deidentification/_model_base.py index 3764c93668ba..1c8c67e86672 100644 --- a/sdk/healthdataaiservices/azure-health-deidentification/azure/health/deidentification/_model_base.py +++ b/sdk/healthdataaiservices/azure-health-deidentification/azure/health/deidentification/_model_base.py @@ -327,20 +327,20 @@ def _get_model(module_name: str, model_name: typing.Union[str, typing.ForwardRef models = {k: v for k, v in sys.modules[module_name].__dict__.items() if isinstance(v, type)} module_end = module_name.rsplit(".", 1)[0] models.update({k: v for k, v in sys.modules[module_end].__dict__.items() if isinstance(v, type)}) - + # Get the actual name string from the ForwardRef if that's what we received if isinstance(model_name, typing.ForwardRef): name_str = typing.cast(str, getattr(model_name, "__forward_arg__", "")) else: name_str = typing.cast(str, model_name) if model_name else "" - + if isinstance(name_str, str): name_str = name_str.split(".")[-1] - + # Look up the model class by name if not isinstance(name_str, str): raise ValueError("Model name must be a string or ForwardRef") - + if name_str not in models: return name_str return models[name_str]