Skip to content

Commit

Permalink
fix generic handling
Browse files Browse the repository at this point in the history
  • Loading branch information
chriskapp committed Apr 7, 2024
1 parent a89027d commit 3c81145
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/Generator/Normalizer/Python.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ protected function getKeywords(): array
'continue',
'global',
'pass',
'async',
];
}
}
22 changes: 20 additions & 2 deletions src/Generator/Python.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,30 @@ protected function newNormalizer(): NormalizerInterface

protected function writeStruct(Code\Name $name, array $properties, ?string $extends, ?array $generics, StructType $origin): string
{
$code = '@dataclass_json' . "\n";
$code = '';
if (!empty($generics)) {
foreach ($generics as $type) {
$code.= $type . ' = TypeVar("' . $type . '")' . "\n";
}
}

$code.= '@dataclass_json' . "\n";
$code.= '@dataclass' . "\n";
$code.= 'class ' . $name->getClass();

$parts = [];
if (!empty($extends)) {
$code.= '(' . $extends . ')';
$parts[] = $extends;
}

if (!empty($generics)) {
foreach ($generics as $type) {
$parts[] = 'Generic[' . $type . ']';
}
}

if (!empty($parts)) {
$code.= '(' . implode(', ', $parts) . ')';
}

$code.= ':' . "\n";
Expand Down
3 changes: 2 additions & 1 deletion tests/Generator/resource/python/python_oop.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ class StudentMap(Map[Student]):
from dataclasses import dataclass, field
from dataclasses_json import dataclass_json, config
from typing import List
T = TypeVar("T")
@dataclass_json
@dataclass
class Map:
class Map(Generic[T]):
total_results: int = field(default=None, metadata=config(field_name="totalResults"))
entries: List[T] = field(default=None, metadata=config(field_name="entries"))

Expand Down

0 comments on commit 3c81145

Please sign in to comment.