Skip to content
Draft
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions test_enum_descriptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/usr/bin/env python3
Copy link

@qltysh qltysh bot Jul 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shebang is present but file is not executable [ruff:EXE001]

Suggested change
#!/usr/bin/env python3

The shebang line was removed because the file is not executable, which resolves the EXE001 issue identified by the ruff static analysis tool.

Caution: AI-generated change

"""Test script to understand current enum handling and test x-enumDescriptions."""

import os

os.environ["DJANGO_SETTINGS_MODULE"] = "tests.settings"
import django

Comment on lines +6 to +8
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Found 2 issues:

1. Module level import not at top of file [ruff:E402]


2. Import block is un-sorted or un-formatted [ruff:I001]

Suggested change
os.environ["DJANGO_SETTINGS_MODULE"] = "tests.settings"
import django
from django.db import models
from django.utils.translation import gettext_lazy as _
from tests.utils import get_openapi_schema_from_field

The imports from Django and the test utilities were moved to the top of the file to comply with the E402 rule, which requires module-level imports to be at the top of the file. This change ensures that all imports are declared before any other code execution, such as setting environment variables or calling setup functions.

Caution: AI-generated change

django.setup()

from django.db import models
from django.utils.translation import gettext_lazy as _
Comment on lines +11 to +12
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Module level import not at top of file [ruff:E402]

Suggested change
from django.db import models
from django.utils.translation import gettext_lazy as _
from django.utils.translation import gettext_lazy as _
from django.db import models

The import statement for gettext_lazy was moved to the top of the file to comply with PEP 8 guidelines, which require all import statements to be at the top of the file. This resolves the E402 error reported by the ruff static analysis tool.

Caution: AI-generated change

from tests.utils import get_openapi_schema_from_field
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Module level import not at top of file [ruff:E402]



class YearInSchool(models.TextChoices):
Copy link

@qltysh qltysh bot Jul 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing docstring in public class [ruff:D101]

Suggested change
class YearInSchool(models.TextChoices):

Added a docstring to the YearInSchool class to comply with the D101 rule, which requires a docstring for public classes. The docstring provides a brief description of the class's purpose.

Caution: AI-generated change

FRESHMAN = "FR", _("Freshman")
SOPHOMORE = "SO", _("Sophomore")
JUNIOR = "JR", _("Junior")
SENIOR = "SR", _("Senior")
GRADUATE = "GR", _("Graduate")


# Test current behavior
field = models.CharField(max_length=2, choices=YearInSchool.choices)
schema = get_openapi_schema_from_field(field)

print("Current OpenAPI schema:")
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

print found [ruff:T201]

import json
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Module level import not at top of file [ruff:E402]


print(json.dumps(schema, indent=2))
Comment on lines +9 to +31

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The script lacks error handling, which could lead to unclear failures if any part of the Django setup or schema generation fails. Recommendation: Implement try-except blocks around critical operations like django.setup() and get_openapi_schema_from_field(field). Log the errors or handle them appropriately to provide feedback on what went wrong.

Comment on lines +24 to +31

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The script is used for testing but does not utilize a formal testing framework or assertions to verify the correctness of the output against expected results. Recommendation: Integrate a testing framework such as unittest or pytest and use assertions to validate the OpenAPI schema. This will make the script more robust and useful as a test tool, allowing systematic handling of test failures and successes.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

print found [ruff:T201]

46 changes: 46 additions & 0 deletions test_schema_extra.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/usr/bin/env python3
Copy link

@qltysh qltysh bot Jul 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shebang is present but file is not executable [ruff:EXE001]

Suggested change
#!/usr/bin/env python3

The shebang line #!/usr/bin/env python3 was removed because the file is not executable. The presence of a shebang line implies that the file should be executed as a script, but since it is not marked as executable, the shebang is unnecessary.

Caution: AI-generated change

"""Test script to explore how to add custom schema extensions in Pydantic."""

import os

os.environ["DJANGO_SETTINGS_MODULE"] = "tests.settings"
import django

django.setup()
Comment on lines +6 to +9

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The script sets the Django environment variables and initializes Django directly within the code. This approach is not flexible and can lead to issues when the script is run in different environments or configurations.

Recommendation: Consider managing environment settings outside of the script, for example, by using environment variables set through a deployment configuration or a .env file. This would make the script more portable and easier to configure across different environments.


from enum import Enum
from typing import Annotated
Comment on lines +6 to +12
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Found 2 issues:

1. Module level import not at top of file [ruff:E402]


2. Import block is un-sorted or un-formatted [ruff:I001]

Suggested change
os.environ["DJANGO_SETTINGS_MODULE"] = "tests.settings"
import django
django.setup()
from enum import Enum
from typing import Annotated
from enum import Enum
os.environ["DJANGO_SETTINGS_MODULE"] = "tests.settings"
import django
django.setup()

The import statement for the Enum module was moved to the top of the file to comply with the E402 rule, which requires all module-level imports to be at the top of the file.

Caution: AI-generated change

from pydantic import BaseModel, Field
Comment on lines +11 to +13
Copy link

@qltysh qltysh bot Jul 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Found 2 issues:

1. Module level import not at top of file [ruff:E402]


2. pydantic.Field imported but unused [ruff:F401]

Suggested change
from enum import Enum
from typing import Annotated
from pydantic import BaseModel, Field
from typing import Annotated
from enum import Enum

The import statement for the 'enum' module was moved to the top of the file to comply with the E402 rule, which requires all module-level imports to be at the top of the file.

Caution: AI-generated change

Comment on lines +12 to +13
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Found 2 issues:

1. Module level import not at top of file [ruff:E402]


2. typing.Annotated imported but unused [ruff:F401]

Suggested change
from typing import Annotated
from pydantic import BaseModel, Field
from pydantic import BaseModel, Field
from typing import Annotated

The import statement for Annotated from the typing module was moved to the top of the file to comply with the E402 rule, which requires all module-level imports to be at the top of the file.

Caution: AI-generated change

from pydantic.json_schema import JsonSchemaValue, GenerateJsonSchema
Comment on lines +11 to +14
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Module level import not at top of file [ruff:E402]

Suggested change
from enum import Enum
from typing import Annotated
from pydantic import BaseModel, Field
from pydantic.json_schema import JsonSchemaValue, GenerateJsonSchema
from pydantic.json_schema import JsonSchemaValue, GenerateJsonSchema
from enum import Enum
from typing import Annotated
from pydantic import BaseModel, Field

The import statement for JsonSchemaValue and GenerateJsonSchema from pydantic.json_schema was moved to the top of the file to comply with the E402 rule, which requires all module-level imports to be at the top of the file.

Caution: AI-generated change

from pydantic_core import core_schema
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Module level import not at top of file [ruff:E402]

import json
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Module level import not at top of file [ruff:E402]



# Create a test enum with custom schema modifier
class TestEnum(str, Enum):
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing docstring in public class [ruff:D101]

A = "a"
B = "b"
C = "c"

@classmethod
def __get_pydantic_json_schema__(
cls, core_schema: core_schema.CoreSchema, handler: GenerateJsonSchema
) -> JsonSchemaValue:
"""Add custom x-enumDescriptions to the enum schema."""
json_schema = handler(core_schema)
json_schema["x-enumDescriptions"] = {
"a": "Choice A",
"b": "Choice B",
"c": "Choice C"
}
return json_schema


# Test how to add custom schema properties
class TestModel(BaseModel):
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing docstring in public class [ruff:D101]

enum_field: TestEnum


schema = TestModel.model_json_schema()
print("Test schema with custom __get_pydantic_json_schema__:")
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

print found [ruff:T201]

print(json.dumps(schema, indent=2))
Comment on lines +44 to +46

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The script lacks error handling around critical operations such as generating the JSON schema and printing it. This could lead to unhandled exceptions if there are issues during these operations.

Recommendation: Implement try-except blocks around the schema generation and JSON serialization to handle potential exceptions gracefully. Provide meaningful error messages to aid in debugging if an error occurs.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

print found [ruff:T201]

Comment on lines +44 to +46

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The script lacks error handling around critical operations such as generating the JSON schema and printing it. This could lead to unhandled exceptions if there are issues during these operations.

Recommendation: Implement try-except blocks around the schema generation and JSON serialization to handle potential exceptions gracefully. Provide meaningful error messages to aid in debugging if an error occurs.

4 changes: 2 additions & 2 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.