datamodel-code-generator 0.26.4__py3-none-any.whl → 0.26.5__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of datamodel-code-generator might be problematic. Click here for more details.

@@ -53,7 +53,6 @@ from datamodel_code_generator.arguments import DEFAULT_ENCODING, arg_parser, nam
53
53
  from datamodel_code_generator.format import (
54
54
  DatetimeClassType,
55
55
  PythonVersion,
56
- black_find_project_root,
57
56
  is_supported_in_black,
58
57
  )
59
58
  from datamodel_code_generator.parser import LiteralType
@@ -366,6 +365,26 @@ class Config(BaseModel):
366
365
  setattr(self, field_name, getattr(parsed_args, field_name))
367
366
 
368
367
 
368
+ def _get_pyproject_toml_config(source: Path) -> Optional[Dict[str, Any]]:
369
+ """Find and return the [tool.datamodel-codgen] section of the closest
370
+ pyproject.toml if it exists.
371
+ """
372
+
373
+ current_path = source
374
+ while current_path != current_path.parent:
375
+ if (current_path / 'pyproject.toml').is_file():
376
+ pyproject_toml = load_toml(current_path / 'pyproject.toml')
377
+ if 'datamodel-codegen' in pyproject_toml.get('tool', {}):
378
+ return pyproject_toml['tool']['datamodel-codegen']
379
+
380
+ if (current_path / '.git').exists():
381
+ # Stop early if we see a git repository root.
382
+ return None
383
+
384
+ current_path = current_path.parent
385
+ return None
386
+
387
+
369
388
  def main(args: Optional[Sequence[str]] = None) -> Exit:
370
389
  """Main function."""
371
390
 
@@ -383,16 +402,9 @@ def main(args: Optional[Sequence[str]] = None) -> Exit:
383
402
  print(version)
384
403
  exit(0)
385
404
 
386
- root = black_find_project_root((Path().resolve(),))
387
- pyproject_toml_path = root / 'pyproject.toml'
388
- if pyproject_toml_path.is_file():
389
- pyproject_toml: Dict[str, Any] = {
390
- k.replace('-', '_'): v
391
- for k, v in load_toml(pyproject_toml_path)
392
- .get('tool', {})
393
- .get('datamodel-codegen', {})
394
- .items()
395
- }
405
+ pyproject_config = _get_pyproject_toml_config(Path().resolve())
406
+ if pyproject_config is not None:
407
+ pyproject_toml = {k.replace('-', '_'): v for k, v in pyproject_config.items()}
396
408
  else:
397
409
  pyproject_toml = {}
398
410
 
@@ -98,7 +98,7 @@ base_options.add_argument(
98
98
  # ======================================================================================
99
99
  model_options.add_argument(
100
100
  '--allow-extra-fields',
101
- help='Allow to pass extra fields, if this flag is not passed, extra fields are forbidden.',
101
+ help='Allow passing extra fields, if this flag is not passed, extra fields are forbidden.',
102
102
  action='store_true',
103
103
  default=None,
104
104
  )
@@ -43,7 +43,7 @@ class Imports(DefaultDict[Optional[str], Set[str]]):
43
43
 
44
44
  def create_line(self, from_: Optional[str], imports: Set[str]) -> str:
45
45
  if from_:
46
- return f"from {from_} import {', '.join(self._set_alias(from_, imports))}"
46
+ return f'from {from_} import {", ".join(self._set_alias(from_, imports))}'
47
47
  return '\n'.join(f'import {i}' for i in self._set_alias(from_, imports))
48
48
 
49
49
  def dump(self) -> str:
@@ -423,7 +423,7 @@ class DataModel(TemplateBase, Nullable, ABC):
423
423
  def class_name(self, class_name: str) -> None:
424
424
  if '.' in self.reference.name:
425
425
  self.reference.name = (
426
- f"{self.reference.name.rsplit('.', 1)[0]}.{class_name}"
426
+ f'{self.reference.name.rsplit(".", 1)[0]}.{class_name}'
427
427
  )
428
428
  else:
429
429
  self.reference.name = class_name
@@ -82,10 +82,22 @@ class Enum(DataModel):
82
82
 
83
83
  def find_member(self, value: Any) -> Optional[Member]:
84
84
  repr_value = repr(value)
85
- for field in self.fields: # pragma: no cover
86
- if field.default == repr_value:
85
+ # Remove surrounding quotes from the string representation
86
+ str_value = str(value).strip('\'"')
87
+
88
+ for field in self.fields:
89
+ # Remove surrounding quotes from field default value
90
+ field_default = field.default.strip('\'"')
91
+
92
+ # Compare values after removing quotes
93
+ if field_default == str_value:
87
94
  return self.get_member(field)
88
- return None # pragma: no cover
95
+
96
+ # Keep original comparison for backwards compatibility
97
+ if field.default == repr_value: # pragma: no cover
98
+ return self.get_member(field)
99
+
100
+ return None
89
101
 
90
102
  @property
91
103
  def imports(self) -> Tuple[Import, ...]:
@@ -65,6 +65,7 @@ def get_special_path(keyword: str, path: List[str]) -> List[str]:
65
65
 
66
66
  escape_characters = str.maketrans(
67
67
  {
68
+ '\u0000': r'\x00', # Null byte
68
69
  '\\': r'\\',
69
70
  "'": r'\'',
70
71
  '\b': r'\b',
@@ -338,7 +338,7 @@ def _get_type(type_: str, format__: Optional[str] = None) -> Types:
338
338
  if data_formats is not None:
339
339
  return data_formats
340
340
 
341
- warn(f'format of {format__!r} not understood for {type_!r} - using default' '')
341
+ warn(f'format of {format__!r} not understood for {type_!r} - using default')
342
342
  return json_schema_data_formats[type_]['default']
343
343
 
344
344
 
@@ -473,7 +473,7 @@ class ModelResolver:
473
473
  else:
474
474
  joined_path = self.join_path(path)
475
475
  if joined_path == '#':
476
- return f"{'/'.join(self.current_root)}#"
476
+ return f'{"/".join(self.current_root)}#'
477
477
  if (
478
478
  self.current_base_path
479
479
  and not self.base_url
@@ -498,7 +498,7 @@ class ModelResolver:
498
498
 
499
499
  delimiter = joined_path.index('#')
500
500
  file_path = ''.join(joined_path[:delimiter])
501
- ref = f"{''.join(joined_path[:delimiter])}#{''.join(joined_path[delimiter + 1:])}"
501
+ ref = f'{"".join(joined_path[:delimiter])}#{"".join(joined_path[delimiter + 1 :])}'
502
502
  if self.root_id_base_path and not (
503
503
  is_url(joined_path) or Path(self._base_path, file_path).is_file()
504
504
  ):
@@ -362,22 +362,21 @@ class DataType(_BaseModel):
362
362
 
363
363
  @property
364
364
  def imports(self) -> Iterator[Import]:
365
+ # Add base import if exists
365
366
  if self.import_:
366
367
  yield self.import_
368
+
369
+ # Define required imports based on type features and conditions
367
370
  imports: Tuple[Tuple[bool, Import], ...] = (
368
371
  (self.is_optional and not self.use_union_operator, IMPORT_OPTIONAL),
369
372
  (len(self.data_types) > 1 and not self.use_union_operator, IMPORT_UNION),
370
- )
371
- if any(self.literals):
372
- import_literal = (
373
+ (
374
+ bool(self.literals),
373
375
  IMPORT_LITERAL
374
376
  if self.python_version.has_literal_type
375
- else IMPORT_LITERAL_BACKPORT
376
- )
377
- imports = (
378
- *imports,
379
- (any(self.literals), import_literal),
380
- )
377
+ else IMPORT_LITERAL_BACKPORT,
378
+ ),
379
+ )
381
380
 
382
381
  if self.use_generic_container:
383
382
  if self.use_standard_collections:
@@ -401,10 +400,13 @@ class DataType(_BaseModel):
401
400
  (self.is_set, IMPORT_SET),
402
401
  (self.is_dict, IMPORT_DICT),
403
402
  )
403
+
404
+ # Yield imports based on conditions
404
405
  for field, import_ in imports:
405
406
  if field and import_ != self.import_:
406
407
  yield import_
407
408
 
409
+ # Propagate imports from any dict_key type
408
410
  if self.dict_key:
409
411
  yield from self.dict_key.imports
410
412
 
@@ -463,7 +465,7 @@ class DataType(_BaseModel):
463
465
  elif len(self.data_types) == 1:
464
466
  type_ = self.data_types[0].type_hint
465
467
  elif self.literals:
466
- type_ = f"{LITERAL}[{', '.join(repr(literal) for literal in self.literals)}]"
468
+ type_ = f'{LITERAL}[{", ".join(repr(literal) for literal in self.literals)}]'
467
469
  else:
468
470
  if self.reference:
469
471
  type_ = self.reference.short_name
@@ -1 +1 @@
1
- version: str = '0.26.4'
1
+ version: str = '0.26.5'
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: datamodel-code-generator
3
- Version: 0.26.4
3
+ Version: 0.26.5
4
4
  Summary: Datamodel Code Generator
5
5
  Home-page: https://github.com/koxudaxi/datamodel-code-generator
6
6
  License: MIT
@@ -480,7 +480,7 @@ Field customization:
480
480
  Use schema description to populate field docstring
481
481
 
482
482
  Model customization:
483
- --allow-extra-fields Allow to pass extra fields, if this flag is not passed, extra fields
483
+ --allow-extra-fields Allow passing extra fields, if this flag is not passed, extra fields
484
484
  are forbidden.
485
485
  --allow-population-by-field-name
486
486
  Allow population by field name
@@ -505,7 +505,7 @@ Model customization:
505
505
  pydantic: datetime, dataclass: str, ...)
506
506
  --reuse-model Reuse models on the field when a module has the model with the same
507
507
  content
508
- --target-python-version {3.6,3.7,3.8,3.9,3.10,3.11,3.12}
508
+ --target-python-version {3.6,3.7,3.8,3.9,3.10,3.11,3.12,3.13}
509
509
  target python version (default: 3.8)
510
510
  --treat-dot-as-module
511
511
  treat dotted module names as modules
@@ -1,13 +1,13 @@
1
1
  datamodel_code_generator/__init__.py,sha256=OrU_lId2PgS_G7Mhn8ih2AmgGc-CXPMtWDgqONlCzfA,19937
2
- datamodel_code_generator/__main__.py,sha256=pRnaCIrCFL-C-H7D_ng4AusD3vdjj4Gtk2vYUyoV0mA,21333
3
- datamodel_code_generator/arguments.py,sha256=ae-RWX7OMSXgNjjIefawoZ9ct1mQwd98nb3yyhqKDws,16306
2
+ datamodel_code_generator/__main__.py,sha256=QA6GA3USdZRjv47HeFhznM2L3rXiu3-PN3biVt25678,21835
3
+ datamodel_code_generator/arguments.py,sha256=jf5LnhDl6LnCqRs05iAzvnUwt3bFnfUA43PIbv1xhks,16306
4
4
  datamodel_code_generator/format.py,sha256=M2lag7AeB4eIHaTORu1A_RzMdIflINbypoeqsEYEEGY,8904
5
5
  datamodel_code_generator/http.py,sha256=CwLVnXO4_W_fWKJsHnJp6Q_3GuF3qjCjeAe48Ihawrs,714
6
- datamodel_code_generator/imports.py,sha256=RagA3hne8OtrbZwDA7TaORQll6tKIzket-A6ShOWf8s,5728
6
+ datamodel_code_generator/imports.py,sha256=utqG4OCL_z92-nY6qpp_ctZbFvRtrcw2NH-mbT3HeLk,5728
7
7
  datamodel_code_generator/model/__init__.py,sha256=PywJfSVTqeTh74jv0uLRIs1dcVrrO2OXPRoP39udqUM,3514
8
- datamodel_code_generator/model/base.py,sha256=V7E2A89Q1J_K4AFz9ygVMtfJgl6SiQOgA5pN1FPLiHc,14695
8
+ datamodel_code_generator/model/base.py,sha256=NaZJFLzVngLjGbAFn65-Y0lDOQSk_bwxTJUJvDKmoQA,14695
9
9
  datamodel_code_generator/model/dataclass.py,sha256=Ebn48PRvCOCcyKhxYxgYBbRcpIvXko-VomZ6N8gKrLA,5871
10
- datamodel_code_generator/model/enum.py,sha256=HTFMCNoHmJRHUoNqQCat3kLbtdmSVX666BubN3bcF1I,3413
10
+ datamodel_code_generator/model/enum.py,sha256=ftQCZ73JBNiLgBzFl2WzbBTrshlnYXvyapK2WxK0xxE,3835
11
11
  datamodel_code_generator/model/imports.py,sha256=9-JLfcilbRz9LI4Q9_YAVpRdIusULBaLsMhHE_6j0-w,784
12
12
  datamodel_code_generator/model/msgspec.py,sha256=TevwsJDtgEzlpd7TvIpcMZ1HGw6gwLkm6yR86b_w8fY,11514
13
13
  datamodel_code_generator/model/pydantic/__init__.py,sha256=AYMjDCtnV4vweYqe1asTRCYdOo8IGLBhd8pEdxyY8ok,1372
@@ -43,18 +43,18 @@ datamodel_code_generator/model/typed_dict.py,sha256=W1r3NRy8uFkYe3YVnjL9PAGZdGyo
43
43
  datamodel_code_generator/model/types.py,sha256=T3Xxa7MToHXIH1zXHT1P6PzE49aah0IhnwkCbYVc79c,3157
44
44
  datamodel_code_generator/model/union.py,sha256=4LT5E46c2OH1dvQmvRWM7mX1Pziu_oWBHwXsGsylUbY,1791
45
45
  datamodel_code_generator/parser/__init__.py,sha256=zHbw6RPlJC0SAQjb-XyVlyZhcOu5PfYgPidy6jlUM8M,793
46
- datamodel_code_generator/parser/base.py,sha256=0j6bWKv0hBtzgWoG5bOd2YrbCZI7Z9nmqiJFCYzwZqg,61947
46
+ datamodel_code_generator/parser/base.py,sha256=MBwA77ht66rSoQtuXDVRaOQ_LGCaXo9InqJJvpfruP8,61987
47
47
  datamodel_code_generator/parser/graphql.py,sha256=MwaLVmEcYky8CRueOYxB2n5MKU4FOmASy9stKIUGBVo,22417
48
- datamodel_code_generator/parser/jsonschema.py,sha256=x5NrwTDGJ21QqbaSBRMJv6iX0XnoGfe9FG8GaGLIVqc,70881
48
+ datamodel_code_generator/parser/jsonschema.py,sha256=sjqpGMXq-Nym4cF-ML6utBiZMYYS0-4QPTGPUBXS1u8,70878
49
49
  datamodel_code_generator/parser/openapi.py,sha256=UMvLzvlNcNxik9ttPvjBl3PGUBhGhYedKcbGnrSNaIQ,26666
50
50
  datamodel_code_generator/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
51
51
  datamodel_code_generator/pydantic_patch.py,sha256=5UKotLqor9HixzXk463CUevyCcH2zmQljMAPRyTOxiM,570
52
- datamodel_code_generator/reference.py,sha256=GC1MNq8RqbxvWNxCZjTXeQc0Gp9Bp6tuM2xzV-QOv78,26579
53
- datamodel_code_generator/types.py,sha256=Mtp-owOLiNlbbXU9tTFJH0MBFP3UYyFLL4jENdDD6es,19818
52
+ datamodel_code_generator/reference.py,sha256=Vaqni43Z1176rL5L83U6OvUM_btRwafQXW-7aB-LuoU,26580
53
+ datamodel_code_generator/types.py,sha256=vTa7-I_NECFkpaLI0-azHErdmr1txdLDdR7xKx5yhYo,19896
54
54
  datamodel_code_generator/util.py,sha256=OmYaVP0z0fGPnvmZQx63qmdMwFnMAIVHfTwSkExpoKk,2829
55
- datamodel_code_generator/version.py,sha256=AEZHDNPY1s8e-e8dAzmhlWLlyKJsKL0RQW6iWL56XTo,24
56
- datamodel_code_generator-0.26.4.dist-info/LICENSE,sha256=K54Lwc6_jduycsy8oFFjQEeSSuEiqvVIjCGIXOMnuTQ,1068
57
- datamodel_code_generator-0.26.4.dist-info/METADATA,sha256=PBa68YPe7sJyUsVqmhoPKRrC0zBzizvVtNeU7FD9r2Y,24955
58
- datamodel_code_generator-0.26.4.dist-info/WHEEL,sha256=Nq82e9rUAnEjt98J6MlVmMCZb-t9cYE2Ir1kpBmnWfs,88
59
- datamodel_code_generator-0.26.4.dist-info/entry_points.txt,sha256=bykbUWqOCiKfxJPGe8jpNqTqD1NG7uyRmozdnwzu7rk,76
60
- datamodel_code_generator-0.26.4.dist-info/RECORD,,
55
+ datamodel_code_generator/version.py,sha256=TNxw8_M4v5PH0eredAbnMHbocjoh_Mh-PTaI4X2JiFY,24
56
+ datamodel_code_generator-0.26.5.dist-info/LICENSE,sha256=K54Lwc6_jduycsy8oFFjQEeSSuEiqvVIjCGIXOMnuTQ,1068
57
+ datamodel_code_generator-0.26.5.dist-info/METADATA,sha256=1j7kIzH6carcuQiFE5nMo45gOd8G4VNaL5p_hsu9ZS8,24960
58
+ datamodel_code_generator-0.26.5.dist-info/WHEEL,sha256=Nq82e9rUAnEjt98J6MlVmMCZb-t9cYE2Ir1kpBmnWfs,88
59
+ datamodel_code_generator-0.26.5.dist-info/entry_points.txt,sha256=bykbUWqOCiKfxJPGe8jpNqTqD1NG7uyRmozdnwzu7rk,76
60
+ datamodel_code_generator-0.26.5.dist-info/RECORD,,