OpenAPITools / OpenAPITools/openapi-generator

[Question] Python anyof template vendor extension type

Open
#23,111 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Hi,

I'm currently trying to activate mypy on our generated code and have a small issue with a model for an anyof-type.

The generated code contains (template: https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/python/model_anyof.mustache#L24)

    # data type: bytearray
    anyof_schema_4_validator: Optional[Union[StrictBytes, StrictStr, Tuple[StrictStr, StrictBytes]]] = None

The data type of variable actual_instance is (template: https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/python/model_anyof.mustache#L28)

        actual_instance: Optional[Union[...., bytearray, .... ]] = None

So there is a discrepancy between the types of the validator and the actual instance variable. Mypy then complains about code in the from_json method (template: https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/python/model_anyof.mustache#L115):

        try:
            # validation
            instance.anyof_schema_4_validator = json.loads(json_str)
            # assign value to actual_instance
            instance.actual_instance = instance.anyof_schema_4_validator

Here, the generated code assigns bytearray to StrictBytes etc. - this probably works fine, but to please mypy it would be nice to use also the StrictBytes type in the type hint for variable actual_instance (and probably also for the ...ANY_OF_SCHEMAS variable (https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/python/model_anyof.mustache#L16).

Is this already possible with some kind of vendor extension?

Thanks and best regards
oz

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with modules/openapi-generator/src/main/resources/python/model_anyof.mustache, especially lines 16, 24, 28, and 115, and compare the generated validator and actual_instance type hints. Check whether an existing vendor extension controls these types; done means the generated anyof model has consistent mypy-compatible annotations without breaking validation.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.