Skip to content

Commit

Permalink
Merge branch 'main' into add-formatter-module
Browse files Browse the repository at this point in the history
  • Loading branch information
koxudaxi authored Aug 6, 2024
2 parents 2894f63 + f856d13 commit c317d71
Show file tree
Hide file tree
Showing 505 changed files with 9,775 additions and 7,065 deletions.
2 changes: 1 addition & 1 deletion .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# These are supported funding model platforms

github: [koxudaxi]
custom: ['https://stakes.social/0x286DBf93CB471EB3297237b1C66662f6be32B42c']
polar: koxudaxi
25 changes: 25 additions & 0 deletions .github/workflows/codespell.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Codespell configuration is within pyproject.toml
---
name: Codespell

on:
push:
branches: [main]
pull_request:
branches: [main]

permissions:
contents: read

jobs:
codespell:
name: Check for spelling errors
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v4
- name: Codespell
uses: codespell-project/actions-codespell@v2
with:
exclude_file: CODE_OF_CONDUCT.md
2 changes: 1 addition & 1 deletion .github/workflows/codspeed.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
path: .venv
key: venv-${{ runner.os }}-codspeed-3.12-v1-${{ hashFiles('**/poetry.lock') }}
- name: Install dependencies
if: steps.cached-poetry-dependencies.outputs.cache-hit != 'true'
# if: steps.cached-poetry-dependencies.outputs.cache-hit != 'true'
run: poetry install --no-interaction

- name: Run benchmarks
Expand Down
45 changes: 38 additions & 7 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
name: Test

on:
pull_request: {}
push:
branches: [main]
pull_request:
branches: [main]

jobs:
test:
Expand All @@ -21,6 +24,31 @@ jobs:
black-version: default
python-version: 3.8
pydantic-version: 1.8.2
- os: ubuntu-latest
isort-version: 5.6.4
black-version: 19.10b0
python-version: 3.9
pydantic-version: 1.8.2
- os: ubuntu-latest
isort-version: 5.6.4
black-version: 24.1.0
python-version: 3.12
pydantic-version: 2.4.2
- os: ubuntu-latest
isort-version: 5.6.4
black-version: 23.12.1
python-version: 3.12
pydantic-version: 2.4.2
- os: macos-13
isort-version: 5.6.4
black-version: 22.1.0
python-version: 3.7
pydantic-version: 2.4.2
- os: macos-13
isort-version: 5.6.4
black-version: 22.1.0
python-version: 3.7
pydantic-version: 1.10.9
exclude:
- os: windows-latest
black-version: 22.1.0
Expand All @@ -30,6 +58,8 @@ jobs:
isort-version: 4.3.21
- os: macos-latest
isort-version: 4.3.21
- os: macos-latest
python-version: 3.7
- os: windows-latest
pydantic-version: 1.5.1
- os: macos-latest
Expand All @@ -50,7 +80,7 @@ jobs:
PYTHON: ${{ matrix.python-version }}
ISORT: ${{ matrix.isort-version }}
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
Expand Down Expand Up @@ -91,10 +121,10 @@ jobs:
if: matrix.pydantic-version != 'default'
run: |
poetry run pip install pydantic=="${{ matrix.pydantic-version }}"
- name: Install Black 22.1.0
if: matrix.black-version == '22.1.0'
- name: Install Black ${{ matrix.black-version }}
if: matrix.black-version != 'default'
run: |
poetry run pip install black=="22.1.0"
poetry run pip install black=="${{ matrix.black-version }}"
- name: Lint
if: matrix.pydantic-version == 'default'
run: |
Expand All @@ -106,10 +136,11 @@ jobs:
./scripts/test.sh
- name: Upload coverage to Codecov
if: matrix.os == 'ubuntu-latest' && matrix.pydantic-version != '1.5.1'
uses: codecov/codecov-action@v3
uses: codecov/codecov-action@v4
with:
token: ${{ secrets.CODECOV_TOKEN }}
flags: unittests
file: ./coverage.xml
fail_ci_if_error: true
env_vars: OS,PYTHON,ISORT
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -175,3 +175,5 @@ fabric.properties
.idea/caches/build_file_checksums.ser

.idea

.vscode
10 changes: 9 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: 'v0.1.6'
rev: 'v0.5.6'
hooks:
- id: ruff
files: "^datamodel_code_generator|^tests"
Expand All @@ -9,3 +9,11 @@ repos:
- id: ruff-format
files: "^datamodel_code_generator|^tests"
exclude: "^tests/data"
- repo: https://github.com/codespell-project/codespell
# Configuration for codespell is in pyproject.toml
rev: v2.3.0
hooks:
- id: codespell
additional_dependencies:
- tomli
exclude: "^tests/|^CODE_OF_CONDUCT.md"
81 changes: 56 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ and [msgspec.Struct](https://github.com/jcrist/msgspec) from an openapi file and
## Help
See [documentation](https://koxudaxi.github.io/datamodel-code-generator) for more details.

## Sponsors
[![JetBrains](https://avatars.githubusercontent.com/u/60931315?s=200&v=4)](https://github.com/JetBrainsOfficial)

## Quick Installation

To install `datamodel-code-generator`:
Expand Down Expand Up @@ -236,13 +233,52 @@ class Apis(BaseModel):
```
</details>
## Supported input types
- OpenAPI 3 (YAML/JSON, [OpenAPI Data Type](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.2.md#data-types));
- JSON Schema ([JSON Schema Core](http://json-schema.org/draft/2019-09/json-schema-validation.html)/[JSON Schema Validation](http://json-schema.org/draft/2019-09/json-schema-validation.html));
- JSON/YAML/CSV Data (it will be converted to JSON Schema);
- Python dictionary (it will be converted to JSON Schema);
- GraphQL schema ([GraphQL Schemas and Types](https://graphql.org/learn/schema/));
## Supported output types
- [pydantic](https://docs.pydantic.dev/1.10/).BaseModel;
- [pydantic_v2](https://docs.pydantic.dev/2.0/).BaseModel;
- [dataclasses.dataclass](https://docs.python.org/3/library/dataclasses.html);
- [typing.TypedDict](https://docs.python.org/3/library/typing.html#typing.TypedDict);
- [msgspec.Struct](https://github.com/jcrist/msgspec);
- Custom type from your [jinja2](https://jinja.palletsprojects.com/en/3.1.x/) template;
## Sponsors
<table>
<tr>
<td valign="top" align="center">
<a href="https://github.com/JetBrainsOfficial">
<img src="https://avatars.githubusercontent.com/u/60931315?s=100&v=4" alt="JetBrains Logo" style="width: 100px;">
<p>JetBrains</p>
</a>
</td>
<td valign="top" align="center">
<a href="https://github.com/astral-sh">
<img src="https://avatars.githubusercontent.com/u/115962839?s=200&v=4" alt="Astral Logo" style="width: 100px;">
<p>Astral</p>
</a>
</td>
<td valign="top" align="center">
<a href="https://github.com/DataDog">
<img src="https://avatars.githubusercontent.com/u/365230?s=200&v=4" alt="Datadog, Inc. Logo" style="width: 100px;">
<p>Datadog, Inc.</p>
</a>
</td>
</tr>
</table>
## Projects that use datamodel-code-generator
These OSS projects use datamodel-code-generator to generate many models.
See the following linked projects for real world examples and inspiration.
- [airbytehq/airbyte](https://github.com/airbytehq/airbyte)
- *[code-generator/Dockerfile](https://github.com/airbytehq/airbyte/blob/master/tools/code-generator/Dockerfile)*
- *[Generate Python, Java/Kotlin, and Typescript protocol models](https://github.com/airbytehq/airbyte-protocol/tree/main/protocol-models/bin)*
- [apache/iceberg](https://github.com/apache/iceberg)
- *[Generate Python code](https://github.com/apache/iceberg/blob/d2e1094ee0cc6239d43f63ba5114272f59d605d2/open-api/README.md?plain=1#L39)*
*[`make generate`](https://github.com/apache/iceberg/blob/d2e1094ee0cc6239d43f63ba5114272f59d605d2/open-api/Makefile#L24-L34)*
Expand All @@ -267,21 +303,6 @@ See the following linked projects for real world examples and inspiration.
- [SeldonIO/MLServer](https://github.com/SeldonIO/MLServer)
- *[generate-types.sh](https://github.com/SeldonIO/MLServer/blob/master/hack/generate-types.sh)*

## Supported input types
- OpenAPI 3 (YAML/JSON, [OpenAPI Data Type](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.2.md#data-types));
- JSON Schema ([JSON Schema Core](http://json-schema.org/draft/2019-09/json-schema-validation.html)/[JSON Schema Validation](http://json-schema.org/draft/2019-09/json-schema-validation.html));
- JSON/YAML/CSV Data (it will be converted to JSON Schema);
- Python dictionary (it will be converted to JSON Schema);
- GraphQL schema ([GraphQL Schemas and Types](https://graphql.org/learn/schema/));

## Supported output types
- [pydantic](https://docs.pydantic.dev/1.10/).BaseModel;
- [pydantic_v2](https://docs.pydantic.dev/2.0/).BaseModel;
- [dataclasses.dataclass](https://docs.python.org/3/library/dataclasses.html);
- [typing.TypedDict](https://docs.python.org/3/library/typing.html#typing.TypedDict);
- [msgspec.Struct](https://github.com/jcrist/msgspec);
- Custom type from your [jinja2](https://jinja.palletsprojects.com/en/3.1.x/) template;

## Installation

To install `datamodel-code-generator`:
Expand Down Expand Up @@ -319,6 +340,7 @@ This method needs the [http extra option](#http-extra-option)
## All Command Options

The `datamodel-codegen` command:

```bash
usage:
datamodel-codegen [options]
Expand All @@ -329,10 +351,13 @@ Options:
--http-headers HTTP_HEADER [HTTP_HEADER ...]
Set headers in HTTP requests to the remote host.
(example: "Authorization: Basic dXNlcjpwYXNz")
--http-ignore-tls Disable verification of the remote host's TLS
--http-ignore-tls Disable verification of the remote host''s TLS
certificate
--http-query-parameters QUERY_PARAMETER [QUERY_PARAMETER ...]
Set query parameters in HTTP requests to the remote host.
(example: "ref=branch")
--input INPUT Input file/directory (default: stdin)
--input-file-type {auto,openapi,jsonschema,json,yaml,dict,csv}
--input-file-type {auto,openapi,graphql,jsonschema,json,yaml,dict,csv}
Input file type (default: auto)
--output OUTPUT Output file (default: stdout)
--output-model-type {pydantic.BaseModel,pydantic_v2.BaseModel,dataclasses.dataclass,typing.TypedDict,msgspec.Struct}
Expand Down Expand Up @@ -394,14 +419,17 @@ Field customization:
e.g. underscores
--snake-case-field Change camel-case field name to snake-case
--special-field-name-prefix SPECIAL_FIELD_NAME_PREFIX
Set field name prefix when first character can't be
Set field name prefix when first character can''t be
used as Python field name (default: `field`)
--strip-default-none Strip default None on fields
--use-default Use default value even if a field is required
--use-default-kwarg Use `default=` instead of a positional argument for
Fields that have default values.
--use-field-description
Use schema description to populate field docstring
--use-pendulum
Use pendulum instead of `datetime` for `date`,
`datetime`, and `time` data types

Model customization:
--allow-extra-fields Allow to pass extra fields, if this flag is not
Expand All @@ -412,7 +440,7 @@ Model customization:
Set class name of root model
--collapse-root-models
Models generated with a root-type field will be
mergedinto the models using that root-type model
merged into the models using that root-type model
--disable-appending-item-suffix
Disable appending `Item` suffix to model name in an
array
Expand All @@ -421,14 +449,17 @@ Model customization:
Enable faux immutability
--enable-version-header
Enable package version on file headers
--keep-model-order Keep generated models' order
--reuse-model Re-use models on the field when a module has the model
--keep-model-order Keep generated models'' order
--reuse-model Reuse models on the field when a module has the model
with the same content
--target-python-version {3.6,3.7,3.8,3.9,3.10,3.11}
target python version (default: 3.7)
--use-schema-description
Use schema description to populate class docstring
--use-title-as-name use titles as class names of models
--use-exact-imports Import exact types instead of modules, for example:
`from .foo import Bar` instead of
`from . import foo` with `foo.Bar`

Template customization:
--aliases ALIASES Alias mapping file
Expand Down
18 changes: 14 additions & 4 deletions datamodel_code_generator/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

import yaml

import datamodel_code_generator.pydantic_patch # noqa: F401
from datamodel_code_generator.format import PythonVersion
from datamodel_code_generator.parser import DefaultPutDict, LiteralType
from datamodel_code_generator.parser.base import Parser
Expand Down Expand Up @@ -58,8 +59,7 @@ def load_yaml_from_path(path: Path, encoding: str) -> Any:

if TYPE_CHECKING:

def get_version() -> str:
...
def get_version() -> str: ...

else:

Expand Down Expand Up @@ -300,6 +300,10 @@ def generate(
custom_file_header_path: Optional[Path] = None,
custom_formatters: Optional[List[str]] = None,
custom_formatters_kwargs: Optional[Dict[str, Any]] = None,
use_pendulum: bool = False,
http_query_parameters: Optional[Sequence[Tuple[str, str]]] = None,
treat_dots_as_module: bool = False,
use_exact_imports: bool = False,
) -> None:
remote_text_cache: DefaultPutDict[str, str] = DefaultPutDict()
if isinstance(input_, str):
Expand All @@ -309,7 +313,9 @@ def generate(

input_text = remote_text_cache.get_or_put(
input_.geturl(),
default_factory=lambda url: get_body(url, http_headers, http_ignore_tls),
default_factory=lambda url: get_body(
url, http_headers, http_ignore_tls, http_query_parameters
),
)
else:
input_text = None
Expand Down Expand Up @@ -456,6 +462,10 @@ def get_header_and_first_line(csv_file: IO[str]) -> Dict[str, Any]:
known_third_party=data_model_types.known_third_party,
custom_formatters=custom_formatters,
custom_formatters_kwargs=custom_formatters_kwargs,
use_pendulum=use_pendulum,
http_query_parameters=http_query_parameters,
treat_dots_as_module=treat_dots_as_module,
use_exact_imports=use_exact_imports,
**kwargs,
)

Expand Down Expand Up @@ -525,7 +535,7 @@ def infer_input_type(text: str) -> InputFileType:


inferred_message = (
'The input file type was determined to be: {}\nThis can be specificied explicitly with the '
'The input file type was determined to be: {}\nThis can be specified explicitly with the '
'`--input-file-type` option.'
)

Expand Down
Loading

0 comments on commit c317d71

Please sign in to comment.