diff --git a/.chronus/changes/python-drop-39-2026-4-27-6-50-0.md b/.chronus/changes/python-drop-39-2026-4-27-6-50-0.md new file mode 100644 index 00000000000..c35e26f1dcc --- /dev/null +++ b/.chronus/changes/python-drop-39-2026-4-27-6-50-0.md @@ -0,0 +1,8 @@ +--- +# Change versionKind to one of: internal, fix, dependencies, feature, deprecation, breaking +changeKind: deprecation +packages: + - "@typespec/http-client-python" +--- + +Drop support for Python 3.9. The minimum supported Python version is now 3.10. Python 3.9 reached end-of-life and is no longer supported by upstream Python. diff --git a/packages/http-client-python/emitter/src/run-python3.ts b/packages/http-client-python/emitter/src/run-python3.ts index 9336f672672..422a28be733 100644 --- a/packages/http-client-python/emitter/src/run-python3.ts +++ b/packages/http-client-python/emitter/src/run-python3.ts @@ -11,7 +11,7 @@ import { patchPythonPath } from "./system-requirements.js"; export async function runPython3(...args: string[]) { const command = await patchPythonPath(["python", ...args], { - version: ">=3.9", + version: ">=3.10", environmentVariable: "AUTOREST_PYTHON_EXE", }); cp.execSync(command.join(" "), { diff --git a/packages/http-client-python/eng/scripts/ci/config/mypy.ini b/packages/http-client-python/eng/scripts/ci/config/mypy.ini index 829e96d3673..6bd11f1f115 100644 --- a/packages/http-client-python/eng/scripts/ci/config/mypy.ini +++ b/packages/http-client-python/eng/scripts/ci/config/mypy.ini @@ -1,6 +1,6 @@ # global configurations [mypy] -python_version = 3.9 +python_version = 3.10 # Exclude mypy check for sub client tests exclude = .*/clientinitialization/.*\.py diff --git a/packages/http-client-python/eng/scripts/ci/config/pylintrc b/packages/http-client-python/eng/scripts/ci/config/pylintrc index 85b2a1b8f8d..387021335fc 100644 --- a/packages/http-client-python/eng/scripts/ci/config/pylintrc +++ b/packages/http-client-python/eng/scripts/ci/config/pylintrc @@ -1,5 +1,5 @@ [MASTER] -py-version=3.9 +py-version=3.10 ignore-patterns=test_*,conftest,setup reports=no diff --git a/packages/http-client-python/eng/scripts/ci/config/pyrightconfig.json b/packages/http-client-python/eng/scripts/ci/config/pyrightconfig.json index 86b3a380220..ef8340726d5 100644 --- a/packages/http-client-python/eng/scripts/ci/config/pyrightconfig.json +++ b/packages/http-client-python/eng/scripts/ci/config/pyrightconfig.json @@ -2,5 +2,5 @@ "reportUnnecessaryCast": "warning", "reportTypeCommentUsage": true, "reportMissingImports": false, - "pythonVersion": "3.9" + "pythonVersion": "3.10" } diff --git a/packages/http-client-python/eng/scripts/ci/lint.ts b/packages/http-client-python/eng/scripts/ci/lint.ts index 9dad879a992..37c9469fd8a 100644 --- a/packages/http-client-python/eng/scripts/ci/lint.ts +++ b/packages/http-client-python/eng/scripts/ci/lint.ts @@ -169,7 +169,7 @@ async function lintPygenSource(): Promise { "generator/pygen", "--rcfile=eng/scripts/ci/config/pylintrc", "--recursive=y", - "--py-version=3.9", + "--py-version=3.10", ]); } diff --git a/packages/http-client-python/eng/scripts/ci/run_pylint.py b/packages/http-client-python/eng/scripts/ci/run_pylint.py index 5a44df63e9c..c184c56a3de 100644 --- a/packages/http-client-python/eng/scripts/ci/run_pylint.py +++ b/packages/http-client-python/eng/scripts/ci/run_pylint.py @@ -43,7 +43,7 @@ def _single_dir_pylint(mod): "--evaluation=(max(0, 0 if fatal else 10.0 - ((float(5 * error + warning + refactor + convention + info)/ statement) * 10)))", "--output-format=parseable", "--recursive=y", - "--py-version=3.9", + "--py-version=3.10", ] if is_azure: pylint_args.append("--load-plugins=pylint_guidelines_checker") diff --git a/packages/http-client-python/eng/scripts/setup/build_pygen_wheel.py b/packages/http-client-python/eng/scripts/setup/build_pygen_wheel.py index 74687b91167..40416a0991d 100644 --- a/packages/http-client-python/eng/scripts/setup/build_pygen_wheel.py +++ b/packages/http-client-python/eng/scripts/setup/build_pygen_wheel.py @@ -7,8 +7,8 @@ # -------------------------------------------------------------------------- import sys -if not sys.version_info >= (3, 9, 0): - raise Exception("Autorest for Python extension requires Python 3.9 at least") +if not sys.version_info >= (3, 10, 0): + raise Exception("Autorest for Python extension requires Python 3.10 at least") try: from package_manager import detect_package_manager, PackageManagerNotFoundError @@ -18,7 +18,7 @@ raise Exception("Your Python installation doesn't have a suitable package manager (pip or uv) available") -# Now we have a package manager (pip or uv) and Py >= 3.9, go to work +# Now we have a package manager (pip or uv) and Py >= 3.10, go to work from pathlib import Path diff --git a/packages/http-client-python/eng/scripts/setup/install.py b/packages/http-client-python/eng/scripts/setup/install.py index 8a3532f1ab9..487acc67b1c 100644 --- a/packages/http-client-python/eng/scripts/setup/install.py +++ b/packages/http-client-python/eng/scripts/setup/install.py @@ -7,9 +7,9 @@ # -------------------------------------------------------------------------- import sys -if not sys.version_info >= (3, 9, 0): +if not sys.version_info >= (3, 10, 0): print( - "Warning: Autorest for Python extension requires Python 3.9 at least. We will run your code with Pyodide since your Python version isn't adequate." + "Warning: Autorest for Python extension requires Python 3.10 at least. We will run your code with Pyodide since your Python version isn't adequate." ) sys.exit(2) # Exit code 2 for inadequate environment @@ -32,7 +32,7 @@ sys.exit(2) # Exit code 2 for inadequate environment -# Now we have a package manager (uv or pip) and Py >= 3.9, go to work +# Now we have a package manager (uv or pip) and Py >= 3.10, go to work # At this point, both Python and package manager are confirmed to be available # Any failures from here should fail the npm install, not fallback to Pyodide diff --git a/packages/http-client-python/eng/scripts/setup/install.ts b/packages/http-client-python/eng/scripts/setup/install.ts index 18100a6046a..0835e6e4cfd 100644 --- a/packages/http-client-python/eng/scripts/setup/install.ts +++ b/packages/http-client-python/eng/scripts/setup/install.ts @@ -7,7 +7,7 @@ async function main() { try { // First try to resolve Python path pythonCommand = await patchPythonPath(["python", "./eng/scripts/setup/install.py"], { - version: ">=3.9", + version: ">=3.10", environmentVariable: "AUTOREST_PYTHON_EXE", }); } catch (error) { diff --git a/packages/http-client-python/eng/scripts/setup/prepare.py b/packages/http-client-python/eng/scripts/setup/prepare.py index ff405d552df..6e6de3d547c 100644 --- a/packages/http-client-python/eng/scripts/setup/prepare.py +++ b/packages/http-client-python/eng/scripts/setup/prepare.py @@ -7,9 +7,9 @@ # -------------------------------------------------------------------------- import sys -if not sys.version_info >= (3, 9, 0): +if not sys.version_info >= (3, 10, 0): raise Warning( - "Autorest for Python extension requires Python 3.9 at least. We will run your code with Pyodide since your Python version isn't adequate." + "Autorest for Python extension requires Python 3.10 at least. We will run your code with Pyodide since your Python version isn't adequate." ) from pathlib import Path diff --git a/packages/http-client-python/eng/scripts/setup/run-python3.ts b/packages/http-client-python/eng/scripts/setup/run-python3.ts index 9336f672672..422a28be733 100644 --- a/packages/http-client-python/eng/scripts/setup/run-python3.ts +++ b/packages/http-client-python/eng/scripts/setup/run-python3.ts @@ -11,7 +11,7 @@ import { patchPythonPath } from "./system-requirements.js"; export async function runPython3(...args: string[]) { const command = await patchPythonPath(["python", ...args], { - version: ">=3.9", + version: ">=3.10", environmentVariable: "AUTOREST_PYTHON_EXE", }); cp.execSync(command.join(" "), { diff --git a/packages/http-client-python/generator/pygen/codegen/models/client.py b/packages/http-client-python/generator/pygen/codegen/models/client.py index fd0e25538dd..abb4d83c49d 100644 --- a/packages/http-client-python/generator/pygen/codegen/models/client.py +++ b/packages/http-client-python/generator/pygen/codegen/models/client.py @@ -291,11 +291,12 @@ def imports(self, async_mode: bool, **kwargs) -> FileImport: ImportType.SDKCORE, ) - # import for "Self" + # import for "Self" (typing.Self available from Python 3.11+) file_import.add_submodule_import( "typing_extensions", "Self", ImportType.STDLIB, + version_modules=(((3, 11), "typing", None),), ) if self.need_cloud_setting: file_import.add_submodule_import("typing", "cast", ImportType.STDLIB) diff --git a/packages/http-client-python/generator/pygen/codegen/models/model_type.py b/packages/http-client-python/generator/pygen/codegen/models/model_type.py index d0784f81efb..76ef6e8be8d 100644 --- a/packages/http-client-python/generator/pygen/codegen/models/model_type.py +++ b/packages/http-client-python/generator/pygen/codegen/models/model_type.py @@ -5,8 +5,7 @@ # -------------------------------------------------------------------------- from enum import Enum from collections import OrderedDict -from typing import Any, Optional, TYPE_CHECKING, cast -import sys +from typing import Any, Literal, Optional, TYPE_CHECKING, cast from .utils import add_to_pylint_disable, NamespaceType, LOCALS_LENGTH_LIMIT from .base import BaseType from .constant_type import ConstantType @@ -14,11 +13,6 @@ from .imports import FileImport, ImportType, TypingSection from ...utils import NAME_LENGTH_LIMIT -if sys.version_info >= (3, 8): - from typing import Literal -else: - from typing_extensions import Literal # type: ignore - if TYPE_CHECKING: from .code_model import CodeModel diff --git a/packages/http-client-python/generator/pygen/codegen/serializers/general_serializer.py b/packages/http-client-python/generator/pygen/codegen/serializers/general_serializer.py index ce907c55427..55b399bd391 100644 --- a/packages/http-client-python/generator/pygen/codegen/serializers/general_serializer.py +++ b/packages/http-client-python/generator/pygen/codegen/serializers/general_serializer.py @@ -28,7 +28,7 @@ "corehttp": "1.0.0b6", } -MIN_PYTHON_VERSION = "3.9" +MIN_PYTHON_VERSION = "3.10" MAX_PYTHON_VERSION = "3.13" diff --git a/packages/http-client-python/generator/pygen/codegen/serializers/import_serializer.py b/packages/http-client-python/generator/pygen/codegen/serializers/import_serializer.py index 94fecd5e46b..f1a5b93b2f9 100644 --- a/packages/http-client-python/generator/pygen/codegen/serializers/import_serializer.py +++ b/packages/http-client-python/generator/pygen/codegen/serializers/import_serializer.py @@ -50,20 +50,25 @@ def _serialize_import_type(imports: list[ImportModel], delimiter: str) -> str: import_list = [] for module_name in sorted(set(i.module_name for i in imports)): normal_imports = [i for i in imports if i.module_name == module_name and not i.version_modules] - versioned_imports = [i for i in imports if i.module_name == module_name and i.version_modules] if normal_imports: import_list.append(_serialize_package(normal_imports, delimiter)) - for i in versioned_imports: - import_list.append(_serialize_versioned_package(i, delimiter)) return delimiter.join(import_list) +def _serialize_versioned_imports(imports: list[ImportModel], delimiter: str) -> str: + """Serialize all versioned imports together, to be emitted after all regular imports.""" + return delimiter.join(_serialize_versioned_package(i, delimiter) for i in imports if i.version_modules) + + def _get_import_clauses(imports: list[ImportModel], delimiter: str) -> list[str]: import_clause = [] for import_type in ImportType: - imports_with_import_type = [i for i in imports if i.import_type == import_type] + imports_with_import_type = [i for i in imports if i.import_type == import_type and not i.version_modules] if imports_with_import_type: import_clause.append(_serialize_import_type(imports_with_import_type, delimiter)) + versioned = [i for i in imports if i.version_modules] + if versioned: + import_clause.append(_serialize_versioned_imports(versioned, delimiter)) return import_clause @@ -79,6 +84,13 @@ def _add_type_checking_import(self): if any(self.file_import.get_imports_from_section(TypingSection.TYPING)): self.file_import.add_submodule_import("typing", "TYPE_CHECKING", ImportType.STDLIB) + def _add_sys_import_if_needed(self): + all_imports = list(self.file_import.get_imports_from_section(TypingSection.REGULAR)) + list( + self.file_import.get_imports_from_section(TypingSection.TYPING) + ) + if any(i.version_modules for i in all_imports): + self.file_import.add_import("sys", ImportType.STDLIB) + def get_typing_definitions(self) -> str: def declare_definition(type_name: str, type_definition: TypeDefinition) -> list[str]: ret: list[str] = [] @@ -95,6 +107,7 @@ def declare_definition(type_name: str, type_definition: TypeDefinition) -> list[ def __str__(self) -> str: self._add_type_checking_import() + self._add_sys_import_if_needed() regular_imports = "" regular_imports_list = self._get_imports_list(TypingSection.REGULAR) diff --git a/packages/http-client-python/generator/pygen/codegen/templates/model_base.py.jinja2 b/packages/http-client-python/generator/pygen/codegen/templates/model_base.py.jinja2 index 326ff1f4f2c..258b741a8dd 100644 --- a/packages/http-client-python/generator/pygen/codegen/templates/model_base.py.jinja2 +++ b/packages/http-client-python/generator/pygen/codegen/templates/model_base.py.jinja2 @@ -19,17 +19,22 @@ from datetime import datetime, date, time, timedelta, timezone from json import JSONEncoder import xml.etree.ElementTree as ET from collections.abc import MutableMapping -from typing_extensions import Self import isodate from {{ code_model.core_library }}.exceptions import DeserializationError from {{ code_model.core_library }}{{ "" if code_model.is_azure_flavor else ".utils" }} import CaseInsensitiveEnumMeta from {{ code_model.core_library }}.{{ "" if code_model.is_azure_flavor else "runtime." }}pipeline import PipelineResponse from {{ code_model.core_library }}.serialization import _Null + {% if code_model.has_external_type %} from {{ code_model.core_library }}.serialization import TypeHandlerRegistry {% endif %} from {{ code_model.core_library }}.rest import HttpResponse +if sys.version_info >= (3, 11): + from typing import Self +else: + from typing_extensions import Self + _LOGGER = logging.getLogger(__name__) __all__ = ["SdkJSONEncoder", "Model", "rest_field", "rest_discriminator"] @@ -617,7 +622,7 @@ class Model(_MyMutableMapping): class_name = self.__class__.__name__ if len(args) > 1: raise TypeError(f"{class_name}.__init__() takes 2 positional arguments but {len(args) + 1} were given") - dict_to_pass: typing.Dict[str, typing.Any] = {} + dict_to_pass: dict[str, typing.Any] = {} if args: if isinstance(args[0], ET.Element): dict_to_pass.update(self._init_from_xml(args[0])) diff --git a/packages/http-client-python/generator/pygen/codegen/templates/packaging_templates/README.md.jinja2 b/packages/http-client-python/generator/pygen/codegen/templates/packaging_templates/README.md.jinja2 index d95a3a89db8..d40beb74273 100644 --- a/packages/http-client-python/generator/pygen/codegen/templates/packaging_templates/README.md.jinja2 +++ b/packages/http-client-python/generator/pygen/codegen/templates/packaging_templates/README.md.jinja2 @@ -3,7 +3,7 @@ # Microsoft Azure SDK for Python This is the Microsoft {{options["package-pprint-name"]}} Client Library. -This package has been tested with Python 3.9+. +This package has been tested with Python 3.10+. For a more complete view of Azure libraries, see the [azure sdk python release](https://aka.ms/azsdk/python/all). # Usage @@ -40,7 +40,7 @@ python -m pip install {{ options["package-name"] }} #### Prequisites -- Python 3.9 or later is required to use this package. +- Python 3.10 or later is required to use this package. - You need an [Azure subscription][azure_sub] to use this package. - An existing {{ options["package-pprint-name"] }} instance. @@ -121,7 +121,7 @@ This package is generated by `@typespec/http-client-python` with Typespec. ### Prequisites -- Python 3.9 or later is required to use this package. +- Python 3.10 or later is required to use this package. ### Install the package diff --git a/packages/http-client-python/generator/pygen/codegen/templates/serialization.py.jinja2 b/packages/http-client-python/generator/pygen/codegen/templates/serialization.py.jinja2 index 8298b962da3..de51bf9343f 100644 --- a/packages/http-client-python/generator/pygen/codegen/templates/serialization.py.jinja2 +++ b/packages/http-client-python/generator/pygen/codegen/templates/serialization.py.jinja2 @@ -35,11 +35,15 @@ except ImportError: import xml.etree.ElementTree as ET import isodate # type: ignore -from typing_extensions import Self from {{ code_model.core_library }}.exceptions import DeserializationError, SerializationError from {{ code_model.core_library }}.serialization import NULL as CoreNull +if sys.version_info >= (3, 11): + from typing import Self +else: + from typing_extensions import Self + _BOM = codecs.BOM_UTF8.decode(encoding="utf-8") JSON = MutableMapping[str, Any] diff --git a/packages/http-client-python/generator/setup.py b/packages/http-client-python/generator/setup.py index 99996b606f3..f432970c9a7 100644 --- a/packages/http-client-python/generator/setup.py +++ b/packages/http-client-python/generator/setup.py @@ -35,7 +35,6 @@ "Development Status :: 4 - Beta", "Programming Language :: Python", "Programming Language :: Python :: 3", - "Programming Language :: Python :: 3.9", "Programming Language :: Python :: 3.10", "Programming Language :: Python :: 3.11", "Programming Language :: Python :: 3.12",