-
Notifications
You must be signed in to change notification settings - Fork 316
[http-client-python] Preserve custom fields when migrating from setup.py to pyproject.toml #8673
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
Draft
Copilot
wants to merge
14
commits into
main
Choose a base branch
from
copilot/fix-693fde59-8e8e-4f46-ac31-090e6f2731c7
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 9 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
a4baef3
Initial plan
Copilot 9acb7bc
Implement setup.py to pyproject.toml field migration
Copilot b30c80b
Add unit tests for setup.py to pyproject.toml migration
Copilot 96a60f1
Address PR feedback: refactor field keeping methods
Copilot 625a046
Address additional PR feedback
Copilot c3ccdbd
Apply suggestion from @swathipil
swathipil b723460
Update test to match logging format change
Copilot 02e48b2
Update changeKind to 'fix' for http-client-python
swathipil 54ba264
Address PR feedback: preserve pyproject.toml URLs and simplify code
Copilot 5646cda
Address PR feedback: move Azure URL to KEEP_FIELDS and handle mixed q…
Copilot f1770ad
lint
swathipil 63c2ac8
fix test
swathipil 1807395
fix project url quotes not rendering correctly
swathipil 7c0d64f
fix test sensitive word
swathipil File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
8 changes: 8 additions & 0 deletions
8
...s/changes/copilot-fix-693fde59-8e8e-4f46-ac31-090e6f2731c7-2025-9-6-23-58-35.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| --- | ||
| # Change versionKind to one of: internal, fix, dependencies, feature, deprecation, breaking | ||
| changeKind: fix | ||
| packages: | ||
| - "@typespec/http-client-python" | ||
| --- | ||
|
|
||
| [http-client-python] Preserve custom fields when migrating from setup.py to pyproject.toml |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,7 @@ | |
| # license information. | ||
| # -------------------------------------------------------------------------- | ||
| import json | ||
| import logging | ||
| from typing import Any | ||
| import re | ||
| import tomli as tomllib | ||
|
|
@@ -19,6 +20,8 @@ | |
| from .client_serializer import ClientSerializer, ConfigSerializer | ||
| from .base_serializer import BaseSerializer | ||
|
|
||
| _LOGGER = logging.getLogger(__name__) | ||
|
|
||
| VERSION_MAP = { | ||
| "msrest": "0.7.1", | ||
| "isodate": "0.6.1", | ||
|
|
@@ -57,20 +60,20 @@ def _extract_min_dependency(self, s): | |
| m = re.search(r"[>=]=?([\d.]+(?:[a-z]+\d+)?)", s) | ||
| return parse_version(m.group(1)) if m else parse_version("0") | ||
|
|
||
| def _keep_pyproject_fields(self, file_content: str) -> dict: | ||
| def _keep_pyproject_fields(self, file_content: str, params: dict) -> None: | ||
| # Load the pyproject.toml file if it exists and extract fields to keep. | ||
| result: dict = {"KEEP_FIELDS": {}} | ||
| # Mutates params in place. | ||
| try: | ||
| loaded_pyproject_toml = tomllib.loads(file_content) | ||
| except Exception: # pylint: disable=broad-except | ||
| # If parsing the pyproject.toml fails, we assume the it does not exist or is incorrectly formatted. | ||
| return result | ||
| return | ||
|
|
||
| # Keep "azure-sdk-build" and "packaging" configuration | ||
| if "tool" in loaded_pyproject_toml and "azure-sdk-build" in loaded_pyproject_toml["tool"]: | ||
| result["KEEP_FIELDS"]["tool.azure-sdk-build"] = loaded_pyproject_toml["tool"]["azure-sdk-build"] | ||
| params["KEEP_FIELDS"]["tool.azure-sdk-build"] = loaded_pyproject_toml["tool"]["azure-sdk-build"] | ||
| if "packaging" in loaded_pyproject_toml: | ||
| result["KEEP_FIELDS"]["packaging"] = loaded_pyproject_toml["packaging"] | ||
| params["KEEP_FIELDS"]["packaging"] = loaded_pyproject_toml["packaging"] | ||
|
|
||
| # Process dependencies | ||
| if "project" in loaded_pyproject_toml: | ||
|
|
@@ -94,22 +97,120 @@ def _keep_pyproject_fields(self, file_content: str) -> dict: | |
| kept_deps.append(dep) | ||
|
|
||
| if kept_deps: | ||
| result["KEEP_FIELDS"]["project.dependencies"] = kept_deps | ||
| params["KEEP_FIELDS"]["project.dependencies"] = kept_deps | ||
|
|
||
| # Keep optional dependencies | ||
| if "optional-dependencies" in loaded_pyproject_toml["project"]: | ||
| result["KEEP_FIELDS"]["project.optional-dependencies"] = loaded_pyproject_toml["project"][ | ||
| params["KEEP_FIELDS"]["project.optional-dependencies"] = loaded_pyproject_toml["project"][ | ||
| "optional-dependencies" | ||
| ] | ||
|
|
||
| # Check for existing keywords and add to the set | ||
| if "keywords" in loaded_pyproject_toml["project"]: | ||
| existing_keywords = loaded_pyproject_toml["project"]["keywords"] | ||
| if existing_keywords: | ||
| params["KEEP_FIELDS"]["project.keywords"].update(existing_keywords) | ||
|
|
||
| # Keep project URLs | ||
| if "urls" in loaded_pyproject_toml["project"]: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add test in autorest.python PR to check additional project.urls are kept |
||
| if "project.urls" not in params["KEEP_FIELDS"]: | ||
| params["KEEP_FIELDS"]["project.urls"] = {} | ||
| params["KEEP_FIELDS"]["project.urls"].update(loaded_pyproject_toml["project"]["urls"]) | ||
|
|
||
| return result | ||
| def _keep_setuppy_fields(self, setuppy_content: str, params: dict) -> None: | ||
| """Parse setup.py file to extract fields that should be kept when migrating to pyproject.toml. | ||
| Mutates params in place.""" | ||
|
|
||
| _LOGGER.info("Keeping the following fields from setup.py when generating pyproject.toml.") | ||
|
|
||
| # Extract install_requires (dependencies) | ||
| install_requires_match = re.search(r'install_requires\s*=\s*\[(.*?)\]', setuppy_content, re.DOTALL) | ||
| if install_requires_match: | ||
| deps_str = install_requires_match.group(1) | ||
| # Parse the dependencies list | ||
| deps = [] | ||
| for line in deps_str.split('\n'): | ||
| line = line.strip() | ||
| if line and not line.startswith('#'): | ||
| # Remove quotes and trailing comma | ||
| dep = line.strip(',').strip().strip('"').strip("'") | ||
| if dep: | ||
| # Check if this is a tracked dependency | ||
| dep_name = re.split(r"[<>=\[]", dep)[0].strip() | ||
| if dep_name not in VERSION_MAP: | ||
| # Keep non-default dependencies | ||
| deps.append(dep) | ||
| _LOGGER.info(f"Keeping field dependency: {dep}") | ||
| else: | ||
| # For tracked dependencies, check if version is higher than default | ||
| default_version = parse_version(VERSION_MAP[dep_name]) | ||
| dep_version = self._extract_min_dependency(dep) | ||
| if dep_version > default_version: | ||
| VERSION_MAP[dep_name] = str(dep_version) | ||
| _LOGGER.info(f"Keeping field dependency: {dep}") | ||
|
|
||
| if deps: | ||
| if "project.dependencies" not in params["KEEP_FIELDS"]: | ||
| params["KEEP_FIELDS"]["project.dependencies"] = [] | ||
| params["KEEP_FIELDS"]["project.dependencies"].extend(deps) | ||
|
|
||
| # Extract project_urls | ||
| project_urls_match = re.search(r'project_urls\s*=\s*\{(.*?)\}', setuppy_content, re.DOTALL) | ||
| if project_urls_match: | ||
| urls_str = project_urls_match.group(1) | ||
| # Parse the project_urls dict | ||
| for line in urls_str.split('\n'): | ||
| line = line.strip() | ||
| if line and ':' in line: | ||
| # Parse "key": "value" format | ||
| key_val_match = re.search(r'["\']([^"\']+)["\']\s*:\s*["\']([^"\']+)["\']', line) | ||
| if key_val_match: | ||
| key = key_val_match.group(1) | ||
| value = key_val_match.group(2) | ||
| # Keep all URLs (even default Azure SDK URLs) | ||
| if "project.urls" not in params["KEEP_FIELDS"]: | ||
| params["KEEP_FIELDS"]["project.urls"] = {} | ||
| # Add quotes around multi-word keys for TOML compatibility | ||
| formatted_key = f'"{key}"' if ' ' in key else key | ||
swathipil marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| params["KEEP_FIELDS"]["project.urls"][formatted_key] = value | ||
| _LOGGER.info(f"Keeping field project.urls.{key}: {value}") | ||
|
|
||
| # Extract keywords | ||
| keywords_match = re.search(r'keywords\s*=\s*["\']([^"\']+)["\']', setuppy_content) | ||
| if keywords_match: | ||
| keywords_str = keywords_match.group(1) | ||
| # Parse the keywords (comma-separated) | ||
| keywords = [kw.strip() for kw in keywords_str.split(',')] | ||
| # Add keywords to the existing set (no filtering) | ||
| params["KEEP_FIELDS"]["project.keywords"].update(keywords) | ||
| _LOGGER.info(f"Keeping field project.keywords: {keywords}") | ||
|
|
||
| # Check PACKAGE_PPRINT_NAME and warn if different | ||
| pprint_match = re.search(r'PACKAGE_PPRINT_NAME\s*=\s*["\']([^"\']+)["\']', setuppy_content) | ||
| if pprint_match: | ||
| existing_pprint_name = pprint_match.group(1) | ||
| generated_pprint_name = self.code_model.options.get("package-pprint-name", "") | ||
| if existing_pprint_name != generated_pprint_name: | ||
| _LOGGER.warning( | ||
| f"Generated package-pprint-name '{generated_pprint_name}' does not match existing " | ||
| f"PACKAGE_PPRINT_NAME '{existing_pprint_name}'. Ensure the new package-pprint-name is correct, " | ||
| f"otherwise change this value in the tspconfig.yaml." | ||
| ) | ||
|
|
||
| def serialize_package_file(self, template_name: str, file_content: str, **kwargs: Any) -> str: | ||
| def serialize_package_file(self, template_name: str, file_content: str, setuppy_file_content: str = "", **kwargs: Any) -> str: | ||
| template = self.env.get_template(template_name) | ||
|
|
||
| # Add fields to keep from an existing pyproject.toml | ||
| if template_name == "pyproject.toml.jinja2": | ||
| params = self._keep_pyproject_fields(file_content) | ||
| # Initialize params with default keywords | ||
| params: dict = {"KEEP_FIELDS": {"project.keywords": {"azure", "azure sdk"}}} | ||
swathipil marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| # Mutate params with fields from pyproject.toml | ||
| self._keep_pyproject_fields(file_content, params) | ||
|
|
||
| # If setup.py exists, mutate params with fields from it | ||
| if setuppy_file_content: | ||
| self._keep_setuppy_fields(setuppy_file_content, params) | ||
| else: | ||
| params = {} | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.