Skip to content

[python] Add mock API tests for extensible enum with special word member names#9952

Open
Copilot wants to merge 11 commits intomainfrom
copilot/add-test-case-for-pull-9785
Open

[python] Add mock API tests for extensible enum with special word member names#9952
Copilot wants to merge 11 commits intomainfrom
copilot/add-test-case-for-pull-9785

Conversation

Copy link
Contributor

Copilot AI commented Mar 6, 2026

  • Update @typespec/http-specs dependency to 0.1.0-alpha.33-dev.5
  • Add test_extensible_strings tests to azure and unbranded (sync + async)
  • Fix: preprocess/__init__.py — skip pad_reserved_words for enumvalue types to remove incorrect Enum suffix from member names
  • Update tests to use correct member names (models.ExtensibleString.CLASS instead of models.ExtensibleString.CLASSEnum)
  • Regenerate special-words client with the fix applied
  • Add changelog entry for bug fix (python-fix-enum-member-reserved-word-suffix-2026-03-06-12-00-00.md)
  • Fix CI: pygen/codegen/serializers/__init__.py — process namespaces deepest-first so the models folder __init__.py (with enum exports) is never overwritten by a pkgutil namespace init from a deeper sub-namespace that shares the same path (e.g. specialwords.models operation group overwriting the specialwords/models/__init__.py data models init)
  • Add changelog entry for serializer fix (python-fix-namespace-models-init-overwrite-2026-03-06-13-00-00.md)
  • All unit tests pass (122/122, excluding pre-existing test_null_serialization env issue)
  • Code review — no issues
Original prompt

This section details on the original issue you should resolve

<issue_title>[python] add test case for #9785
</issue_title>
<issue_description>follow skill https://github.com/microsoft/typespec/blob/main/.github/skills/python-sdk-spector-mock-api-tests/SKILL.md to write test case for #9785
</issue_description>

Comments on the Issue (you are @copilot in this section)


🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.

@msyyc msyyc marked this pull request as ready for review March 6, 2026 08:40
…d member names

Co-authored-by: msyyc <70930885+msyyc@users.noreply.github.com>
@microsoft-github-policy-service microsoft-github-policy-service bot added the emitter:client:python Issue for the Python client emitter: @typespec/http-client-python label Mar 6, 2026
Copilot AI changed the title [WIP] Add test case for python sdk spector mock api [python] Add mock API tests for extensible enum with special word member names Mar 6, 2026
Copilot finished work on behalf of msyyc March 6, 2026 09:31
@msyyc
Copy link
Contributor

msyyc commented Mar 6, 2026

@copilot There is 1 possible bug:
property of generated enum of "packages/http-client-python/generator/test/azure/generated/special-words/specialwords/models/_enums.py" has suffix Enum which is not right:

class ExtensibleString(str, Enum, metaclass=CaseInsensitiveEnumMeta):
    """Verify enum member names that are special words using extensible enum (union)."""

    ANDEnum = "and"
    """ANDEnum."""
    ASEnum = "as"

We should remove the Enum suffix and you may need to fix /generator/pygen

… names

Co-authored-by: msyyc <70930885+msyyc@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Mar 6, 2026

@copilot There is 1 possible bug:
property of generated enum of "packages/http-client-python/generator/test/azure/generated/special-words/specialwords/models/_enums.py" has suffix ...

Fixed in 67e797d.

Root cause: update_types in preprocess/__init__.py applied pad_reserved_words with PadType.ENUM_CLASS ("Enum") to ALL non-model types, including "enumvalue" types. When processing an enum value like AND, the case-insensitive check "and" in RESERVED_WORDS[PadType.ENUM_CLASS] was True, causing the "Enum" suffix to be appended → ANDEnum.

Fix: added and type["type"] != "enumvalue" to skip reserved-word padding for enum values. Since enum member names are already UPPER_CASE (e.g. AND, CLASS, RETURN) and Python reserved words are all lowercase, there's no naming conflict and no padding is needed.

After the fix: AND = "and", CLASS = "class", etc. — no Enum suffix.

Copilot finished work on behalf of msyyc March 6, 2026 12:10
Co-authored-by: msyyc <70930885+msyyc@users.noreply.github.com>
Copilot finished work on behalf of msyyc March 6, 2026 12:13
@github-actions
Copy link
Contributor

github-actions bot commented Mar 6, 2026

All changed packages have been documented.

  • @typespec/http-client-python
Show changes

@typespec/http-client-python - fix ✏️

Fix extensible enum member names incorrectly getting an Enum suffix when the member name matched a Python reserved word (e.g. ANDEnumAND, CLASSEnumCLASS).

@typespec/http-client-python - internal ✏️

Add mock API test coverage for extensible enum with special word member names in special-words spec.

@msyyc
Copy link
Contributor

msyyc commented Mar 6, 2026

@copilot fix ci failure

Copilot stopped work on behalf of msyyc due to an error March 6, 2026 13:10
@msyyc
Copy link
Contributor

msyyc commented Mar 6, 2026

@copilot Fix the following CI error:

requirements.txt
2026-03-06T12:48:10.1805540Z ci: commands[0] /mnt/vss/_work/1/s/packages/http-client-python/generator/test/unittests> pytest
2026-03-06T12:48:11.2074558Z ============================= test session starts ==============================
2026-03-06T12:48:11.2075610Z platform linux -- Python 3.11.14, pytest-8.3.2, pluggy-1.6.0
2026-03-06T12:48:11.2076152Z cachedir: test/unittests/.tox/ci/.pytest_cache
2026-03-06T12:48:11.2076856Z rootdir: /mnt/vss/_work/1/s/packages/http-client-python/generator
2026-03-06T12:48:11.2077343Z plugins: asyncio-0.14.0
2026-03-06T12:48:11.2077886Z collected 50 items / 1 error
2026-03-06T12:48:11.2078044Z 
2026-03-06T12:48:11.2078429Z ==================================== ERRORS ====================================
2026-03-06T12:48:11.2078811Z _______ ERROR collecting test/unittests/test_model_base_serialization.py _______
2026-03-06T12:48:11.2079170Z test_model_base_serialization.py:26: in <module>
2026-03-06T12:48:11.2079625Z     from specialwords._utils.model_base import (
2026-03-06T12:48:11.2080029Z ../unbranded/generated/special-words/specialwords/__init__.py:9: in <module>
2026-03-06T12:48:11.2080387Z     from ._client import SpecialWordsClient  # type: ignore
2026-03-06T12:48:11.2080804Z ../unbranded/generated/special-words/specialwords/_client.py:14: in <module>
2026-03-06T12:48:11.2081185Z     from .operations import ExtensibleStringsOperations, Operations, ParametersOperations
2026-03-06T12:48:11.2081626Z ../unbranded/generated/special-words/specialwords/operations/__init__.py:9: in <module>
2026-03-06T12:48:11.2082027Z     from ._operations import Operations  # type: ignore
2026-03-06T12:48:11.2082619Z ../unbranded/generated/special-words/specialwords/operations/_operations.py:3626: in <module>
2026-03-06T12:48:11.2083093Z     class ExtensibleStringsOperations:
2026-03-06T12:48:11.2083476Z ../unbranded/generated/special-words/specialwords/operations/_operations.py:3643: in ExtensibleStringsOperations
2026-03-06T12:48:11.2083939Z     def put_extensible_string_value(self, body: Union[str, _models1.ExtensibleString], **kwargs: Any) -> Union[
2026-03-06T12:48:11.2084361Z E   AttributeError: module 'specialwords.models' has no attribute 'ExtensibleString'
2026-03-06T12:48:11.2084794Z =========================== short test summary info ============================
2026-03-06T12:48:11.2085197Z ERROR test_model_base_serialization.py - AttributeError: module 'specialwords...
2026-03-06T12:48:11.2085620Z !!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!

…ares models folder path

Co-authored-by: msyyc <70930885+msyyc@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Mar 6, 2026

@copilot Fix the following CI error:

...

Fixed in 9b100c8.

Root cause: The namespace iteration loop in pygen/codegen/serializers/__init__.py processed namespaces in insertion order (shallow first). For the special-words spec, which has both a specialwords top-level namespace (with the new ExtensibleString enum) and a specialwords.models sub-namespace (from the Models operation group), the loop would:

  1. Process specialwords → write proper models/__init__.py with ExtensibleString export
  2. Process specialwords.modelsoverwrite models/__init__.py with just the pkgutil namespace init

This caused specialwords.models.ExtensibleString to be inaccessible. When importing specialwords, the operations file's class definition body: Union[str, _models1.ExtensibleString] (evaluated at class definition time) raised AttributeError: module 'specialwords.models' has no attribute 'ExtensibleString', which propagated as an ImportError at test_model_base_serialization.py:26.

Fix: Sort namespaces deepest-first before processing so shallower namespaces (which write the proper models folder __init__.py) are processed last and always win over any pkgutil init written by deeper sub-namespaces.

Copilot finished work on behalf of msyyc March 6, 2026 13:46
@pkg-pr-new
Copy link

pkg-pr-new bot commented Mar 9, 2026

Open in StackBlitz

npm i https://pkg.pr.new/@typespec/http-client-python@9952

commit: 4945abd

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

emitter:client:python Issue for the Python client emitter: @typespec/http-client-python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[python] add test case for https://github.com/microsoft/typespec/pull/9785

2 participants