microsoft / microsoft/typespec

[http-client-python] added dependencies are copied over from setup.py to pyproject.toml

Open
#8,400 0 comments 1 reaction 1 assignee Assigned to @Copilot View on GitHub
emitter:client:python
Dominant language
Java
Stars
5.9k
Forks
394
Avg merge
1d 23h
Merged PRs (30d)
104

Description

When moving from setup.py to pyproject.toml, any added dependencies, pprint-name, etc. are not being copied over. For example, in the azure-confidentialledger package [[here](https://github.com/Azure/azure-sdk-for-python/blob/c01e7384c190f2de8f62836d0effc09e3b8156f8/sdk/confidentialledger/azure-confidentialledger/pyproject.toml)], the "cryptography" dependency wasn't copied over from the [[setup.py](https://github.com/Azure/azure-sdk-for-python/blob/eafba5a817470a0010285bfcd970ad5a9ae483a6/sdk/confidentialledger/azure-confidentialledger/setup.py)].

The necessary fields to keep from setup.py are:
- any project_urls
- any extra dependencies (use the same check as in the keep_pyproject_fields to keep the larger version if two of the same dependencies are used)
- extra keywords (there should NOT be duplicate keywords in the pyproject.toml) - example here: https://github.com/Azure/azure-sdk-for-python/blob/b4d65d38d73eb8d182111e2b989cb4308d03a1b2/sdk/formrecognizer/azure-ai-formrecognizer/setup.py#L47

Fields that we don't want to keep, BUT we want to log a warning if the generated field is not the same as the existing setup.py field. We can add the logger warnings in the keep_setuppy_fields method:
- package pprint name - example here: https://github.com/Azure/azure-sdk-for-python/blob/eafba5a817470a0010285bfcd970ad5a9ae483a6/sdk/confidentialledger/azure-confidentialledger/setup.py#L19

To fix:
1. Move the if/elif block from [here](https://github.com/microsoft/typespec/blob/cbbf90ec49b1f9a5abcf36eb25f70df41984615f/packages/http-client-python/generator/pygen/codegen/serializers/__init__.py#L139) to after the block [here](https://github.com/microsoft/typespec/blob/cbbf90ec49b1f9a5abcf36eb25f70df41984615f/packages/http-client-python/generator/pygen/codegen/serializers/__init__.py#L147) so that the setup.py file is not removed before we can read it, keep necessary fields, and serialize packaging files including pyproject.toml
2. Modify [this if statement](https://github.com/microsoft/typespec/blob/cbbf90ec49b1f9a5abcf36eb25f70df41984615f/packages/http-client-python/generator/pygen/codegen/serializers/__init__.py#L258). If file == pyproject.toml, then: read_file to get file content and also check to see if `setup.py` exists at the same path and read the file content from that. Otherwise, both should be empty strings. Pass in `file_content` and `setuppy_file_content` into [`serialize_package_file`](https://github.com/microsoft/typespec/blob/cbbf90ec49b1f9a5abcf36eb25f70df41984615f/packages/http-client-python/generator/pygen/codegen/serializers/__init__.py#L261).
3. Add a `_keep_setuppy_fields(...)` method, similar to [`_keep_pyproject_fields`](https://github.com/microsoft/typespec/blob/2641aa9d6f0412d07844216933c50e98a1e9fe21/packages/http-client-python/generator/pygen/codegen/serializers/general_serializer.py#L60) which parses the setup.py for necessary fields. Note:
- At the top of the method, add an INFO level log: `Keeping the following fields from setup.py when generating pyproject.toml.`
- For each field kept, add an INFO level log: `Keeping field {FIELD}: {FIELD value} {optional version if dependency field}`.
- For generated fields that don't match the existing setup.py that we DON'T want to keep, just add a logger warning like: `Generated package_pprint_name does not match existing PACKAGE_PPRINT_NAME. Ensure the new package-pprint-name is correct, otherwise change this value in the tspconfig.yaml.`
4. If setuppy_file_content is not "", [this line](https://github.com/microsoft/typespec/blob/cbbf90ec49b1f9a5abcf36eb25f70df41984615f/packages/http-client-python/generator/pygen/codegen/serializers/general_serializer.py#L112) can then call the `_keep_setuppy_fields` and pass in setuppy_file_content. `params` should append the returned keep setuppy fields.
5. Modify the pyproject.toml template to use the new keep fields if they exist.
6. Update the README.md.jinja2 template to use pyproject.toml here instead of setup.py: https://github.com/microsoft/typespec/blob/3b9100cf34ce15001d86e3b44a983a26c7dcfd4f/packages/http-client-python/generator/pygen/codegen/templates/packaging_templates/README.md.jinja2#L120

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.