Skip to content

Commit

Permalink
fix python external import
Browse files Browse the repository at this point in the history
  • Loading branch information
chriskapp committed Nov 23, 2024
1 parent 67ed589 commit 271a659
Show file tree
Hide file tree
Showing 35 changed files with 90 additions and 79 deletions.
17 changes: 14 additions & 3 deletions src/Generator/Python.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

namespace PSX\Schema\Generator;

use PSX\Schema\DefinitionsInterface;
use PSX\Schema\Exception\GeneratorException;
use PSX\Schema\Format;
use PSX\Schema\Generator\Normalizer\NormalizerInterface;
use PSX\Schema\Generator\Type\GeneratorInterface;
Expand Down Expand Up @@ -147,9 +149,9 @@ private function getImports(DefinitionTypeAbstract $origin): array
{
$imports = [];

$imports[] = 'from pydantic import BaseModel, Field, GetCoreSchemaHandler';
$imports[] = 'from pydantic import BaseModel, Field, GetCoreSchemaHandler, Tag';
$imports[] = 'from pydantic_core import CoreSchema, core_schema';
$imports[] = 'from typing import Any, Dict, Generic, List, Optional, TypeVar';
$imports[] = 'from typing import Any, Dict, Generic, List, Optional, TypeVar, Annotated, Union';

if ($origin instanceof MapDefinitionType) {
$imports[] = 'from collections import UserDict';
Expand All @@ -168,7 +170,16 @@ private function getImports(DefinitionTypeAbstract $origin): array
$refs = TypeUtil::findRefs($origin);
foreach ($refs as $ref) {
[$ns, $name] = TypeUtil::split($ref);
$imports[] = 'from .' . $this->normalizer->file($name) . ' import ' . $this->normalizer->class($name);

if ($ns === DefinitionsInterface::SELF_NAMESPACE) {
$imports[] = 'from .' . $this->normalizer->file($name) . ' import ' . $this->normalizer->class($name);
} else {
if (!isset($this->mapping[$ns])) {
throw new GeneratorException('Provided namespace "' . $ns . '" is not configured');
}

$imports[] = 'from ' . $this->mapping[$ns] . ' import ' . $this->normalizer->class($name);
}
}

return array_merge($imports, $this->getDiscriminatorImports($origin));
Expand Down
2 changes: 1 addition & 1 deletion src/Generator/Type/Python.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,6 @@ protected function getAny(): string

protected function getNamespaced(string $namespace, string $name): string
{
return $namespace . '.' . $name;
return $name;
}
}
4 changes: 2 additions & 2 deletions tests/Generator/PythonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function testGenerateOOP()

public function testGenerateImport()
{
$generator = new Python();
$generator = new Python(Config::of('app', ['my_import' => 'my.import']));

$chunks = $generator->generate($this->getImportSchema());
$this->write($generator, $chunks, __DIR__ . '/resource/python/import');
Expand All @@ -74,7 +74,7 @@ public function testGenerateImport()

public function testGenerateImportNamespace()
{
$generator = new Python(Config::of('Foo.Bar', ['my_import' => 'My.Import']));
$generator = new Python(Config::of('app', ['my_import' => 'my.import']));

$chunks = $generator->generate($this->getImportSchema());
$this->write($generator, $chunks, __DIR__ . '/resource/python/namespace');
Expand Down
4 changes: 2 additions & 2 deletions tests/Generator/resource/python/complex/any_property_type.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from pydantic import BaseModel, Field, GetCoreSchemaHandler
from pydantic import BaseModel, Field, GetCoreSchemaHandler, Tag
from pydantic_core import CoreSchema, core_schema
from typing import Any, Dict, Generic, List, Optional, TypeVar
from typing import Any, Dict, Generic, List, Optional, TypeVar, Annotated, Union
from .property_type import PropertyType


Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from pydantic import BaseModel, Field, GetCoreSchemaHandler
from pydantic import BaseModel, Field, GetCoreSchemaHandler, Tag
from pydantic_core import CoreSchema, core_schema
from typing import Any, Dict, Generic, List, Optional, TypeVar
from typing import Any, Dict, Generic, List, Optional, TypeVar, Annotated, Union
from .collection_definition_type import CollectionDefinitionType


Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from pydantic import BaseModel, Field, GetCoreSchemaHandler
from pydantic import BaseModel, Field, GetCoreSchemaHandler, Tag
from pydantic_core import CoreSchema, core_schema
from typing import Any, Dict, Generic, List, Optional, TypeVar
from typing import Any, Dict, Generic, List, Optional, TypeVar, Annotated, Union
from .collection_property_type import CollectionPropertyType


Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from pydantic import BaseModel, Field, GetCoreSchemaHandler
from pydantic import BaseModel, Field, GetCoreSchemaHandler, Tag
from pydantic_core import CoreSchema, core_schema
from typing import Any, Dict, Generic, List, Optional, TypeVar
from typing import Any, Dict, Generic, List, Optional, TypeVar, Annotated, Union
from .scalar_property_type import ScalarPropertyType


Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from pydantic import BaseModel, Field, GetCoreSchemaHandler
from pydantic import BaseModel, Field, GetCoreSchemaHandler, Tag
from pydantic_core import CoreSchema, core_schema
from typing import Any, Dict, Generic, List, Optional, TypeVar
from typing import Any, Dict, Generic, List, Optional, TypeVar, Annotated, Union
from .array_definition_type import ArrayDefinitionType
from .map_definition_type import MapDefinitionType
from .definition_type import DefinitionType
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from pydantic import BaseModel, Field, GetCoreSchemaHandler
from pydantic import BaseModel, Field, GetCoreSchemaHandler, Tag
from pydantic_core import CoreSchema, core_schema
from typing import Any, Dict, Generic, List, Optional, TypeVar
from typing import Any, Dict, Generic, List, Optional, TypeVar, Annotated, Union
from .array_property_type import ArrayPropertyType
from .map_property_type import MapPropertyType
from .property_type import PropertyType
Expand Down
4 changes: 2 additions & 2 deletions tests/Generator/resource/python/complex/definition_type.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from pydantic import BaseModel, Field, GetCoreSchemaHandler
from pydantic import BaseModel, Field, GetCoreSchemaHandler, Tag
from pydantic_core import CoreSchema, core_schema
from typing import Any, Dict, Generic, List, Optional, TypeVar
from typing import Any, Dict, Generic, List, Optional, TypeVar, Annotated, Union
from .array_definition_type import ArrayDefinitionType
from .map_definition_type import MapDefinitionType
from .struct_definition_type import StructDefinitionType
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from pydantic import BaseModel, Field, GetCoreSchemaHandler
from pydantic import BaseModel, Field, GetCoreSchemaHandler, Tag
from pydantic_core import CoreSchema, core_schema
from typing import Any, Dict, Generic, List, Optional, TypeVar
from typing import Any, Dict, Generic, List, Optional, TypeVar, Annotated, Union
from .property_type import PropertyType


Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from pydantic import BaseModel, Field, GetCoreSchemaHandler
from pydantic import BaseModel, Field, GetCoreSchemaHandler, Tag
from pydantic_core import CoreSchema, core_schema
from typing import Any, Dict, Generic, List, Optional, TypeVar
from typing import Any, Dict, Generic, List, Optional, TypeVar, Annotated, Union
from .scalar_property_type import ScalarPropertyType


Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from pydantic import BaseModel, Field, GetCoreSchemaHandler
from pydantic import BaseModel, Field, GetCoreSchemaHandler, Tag
from pydantic_core import CoreSchema, core_schema
from typing import Any, Dict, Generic, List, Optional, TypeVar
from typing import Any, Dict, Generic, List, Optional, TypeVar, Annotated, Union
from .collection_definition_type import CollectionDefinitionType


Expand Down
4 changes: 2 additions & 2 deletions tests/Generator/resource/python/complex/map_property_type.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from pydantic import BaseModel, Field, GetCoreSchemaHandler
from pydantic import BaseModel, Field, GetCoreSchemaHandler, Tag
from pydantic_core import CoreSchema, core_schema
from typing import Any, Dict, Generic, List, Optional, TypeVar
from typing import Any, Dict, Generic, List, Optional, TypeVar, Annotated, Union
from .collection_property_type import CollectionPropertyType


Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from pydantic import BaseModel, Field, GetCoreSchemaHandler
from pydantic import BaseModel, Field, GetCoreSchemaHandler, Tag
from pydantic_core import CoreSchema, core_schema
from typing import Any, Dict, Generic, List, Optional, TypeVar
from typing import Any, Dict, Generic, List, Optional, TypeVar, Annotated, Union
from .scalar_property_type import ScalarPropertyType


Expand Down
4 changes: 2 additions & 2 deletions tests/Generator/resource/python/complex/property_type.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from pydantic import BaseModel, Field, GetCoreSchemaHandler
from pydantic import BaseModel, Field, GetCoreSchemaHandler, Tag
from pydantic_core import CoreSchema, core_schema
from typing import Any, Dict, Generic, List, Optional, TypeVar
from typing import Any, Dict, Generic, List, Optional, TypeVar, Annotated, Union
from .any_property_type import AnyPropertyType
from .array_property_type import ArrayPropertyType
from .boolean_property_type import BooleanPropertyType
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from pydantic import BaseModel, Field, GetCoreSchemaHandler
from pydantic import BaseModel, Field, GetCoreSchemaHandler, Tag
from pydantic_core import CoreSchema, core_schema
from typing import Any, Dict, Generic, List, Optional, TypeVar
from typing import Any, Dict, Generic, List, Optional, TypeVar, Annotated, Union
from .property_type import PropertyType


Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from pydantic import BaseModel, Field, GetCoreSchemaHandler
from pydantic import BaseModel, Field, GetCoreSchemaHandler, Tag
from pydantic_core import CoreSchema, core_schema
from typing import Any, Dict, Generic, List, Optional, TypeVar
from typing import Any, Dict, Generic, List, Optional, TypeVar, Annotated, Union
from .boolean_property_type import BooleanPropertyType
from .integer_property_type import IntegerPropertyType
from .number_property_type import NumberPropertyType
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from pydantic import BaseModel, Field, GetCoreSchemaHandler
from pydantic import BaseModel, Field, GetCoreSchemaHandler, Tag
from pydantic_core import CoreSchema, core_schema
from typing import Any, Dict, Generic, List, Optional, TypeVar
from typing import Any, Dict, Generic, List, Optional, TypeVar, Annotated, Union
from .scalar_property_type import ScalarPropertyType


Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from pydantic import BaseModel, Field, GetCoreSchemaHandler
from pydantic import BaseModel, Field, GetCoreSchemaHandler, Tag
from pydantic_core import CoreSchema, core_schema
from typing import Any, Dict, Generic, List, Optional, TypeVar
from typing import Any, Dict, Generic, List, Optional, TypeVar, Annotated, Union
from .definition_type import DefinitionType
from .reference_property_type import ReferencePropertyType
from .property_type import PropertyType
Expand Down
4 changes: 2 additions & 2 deletions tests/Generator/resource/python/complex/type_schema.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from pydantic import BaseModel, Field, GetCoreSchemaHandler
from pydantic import BaseModel, Field, GetCoreSchemaHandler, Tag
from pydantic_core import CoreSchema, core_schema
from typing import Any, Dict, Generic, List, Optional, TypeVar
from typing import Any, Dict, Generic, List, Optional, TypeVar, Annotated, Union
from .definition_type import DefinitionType


Expand Down
4 changes: 2 additions & 2 deletions tests/Generator/resource/python/default/author.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from pydantic import BaseModel, Field, GetCoreSchemaHandler
from pydantic import BaseModel, Field, GetCoreSchemaHandler, Tag
from pydantic_core import CoreSchema, core_schema
from typing import Any, Dict, Generic, List, Optional, TypeVar
from typing import Any, Dict, Generic, List, Optional, TypeVar, Annotated, Union
from .location import Location


Expand Down
4 changes: 2 additions & 2 deletions tests/Generator/resource/python/default/location.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from pydantic import BaseModel, Field, GetCoreSchemaHandler
from pydantic import BaseModel, Field, GetCoreSchemaHandler, Tag
from pydantic_core import CoreSchema, core_schema
from typing import Any, Dict, Generic, List, Optional, TypeVar
from typing import Any, Dict, Generic, List, Optional, TypeVar, Annotated, Union


# Location of the person
Expand Down
4 changes: 2 additions & 2 deletions tests/Generator/resource/python/default/meta.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from pydantic import BaseModel, Field, GetCoreSchemaHandler
from pydantic import BaseModel, Field, GetCoreSchemaHandler, Tag
from pydantic_core import CoreSchema, core_schema
from typing import Any, Dict, Generic, List, Optional, TypeVar
from typing import Any, Dict, Generic, List, Optional, TypeVar, Annotated, Union
from collections import UserDict


Expand Down
4 changes: 2 additions & 2 deletions tests/Generator/resource/python/default/news.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from pydantic import BaseModel, Field, GetCoreSchemaHandler
from pydantic import BaseModel, Field, GetCoreSchemaHandler, Tag
from pydantic_core import CoreSchema, core_schema
from typing import Any, Dict, Generic, List, Optional, TypeVar
from typing import Any, Dict, Generic, List, Optional, TypeVar, Annotated, Union
import datetime
from .meta import Meta
from .author import Author
Expand Down
8 changes: 4 additions & 4 deletions tests/Generator/resource/python/import/import.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from pydantic import BaseModel, Field, GetCoreSchemaHandler
from pydantic import BaseModel, Field, GetCoreSchemaHandler, Tag
from pydantic_core import CoreSchema, core_schema
from typing import Any, Dict, Generic, List, Optional, TypeVar
from .student_map import StudentMap
from .student import Student
from typing import Any, Dict, Generic, List, Optional, TypeVar, Annotated, Union
from my.import import StudentMap
from my.import import Student


class Import(BaseModel):
Expand Down
6 changes: 3 additions & 3 deletions tests/Generator/resource/python/import/my_map.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from pydantic import BaseModel, Field, GetCoreSchemaHandler
from pydantic import BaseModel, Field, GetCoreSchemaHandler, Tag
from pydantic_core import CoreSchema, core_schema
from typing import Any, Dict, Generic, List, Optional, TypeVar
from .student import Student
from typing import Any, Dict, Generic, List, Optional, TypeVar, Annotated, Union
from my.import import Student


class MyMap(Student):
Expand Down
12 changes: 6 additions & 6 deletions tests/Generator/resource/python/namespace/import.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
from pydantic import BaseModel, Field, GetCoreSchemaHandler
from pydantic import BaseModel, Field, GetCoreSchemaHandler, Tag
from pydantic_core import CoreSchema, core_schema
from typing import Any, Dict, Generic, List, Optional, TypeVar
from .student_map import StudentMap
from .student import Student
from typing import Any, Dict, Generic, List, Optional, TypeVar, Annotated, Union
from my.import import StudentMap
from my.import import Student


class Import(BaseModel):
students: Optional[My.Import.StudentMap] = Field(default=None, alias="students")
student: Optional[My.Import.Student] = Field(default=None, alias="student")
students: Optional[StudentMap] = Field(default=None, alias="students")
student: Optional[Student] = Field(default=None, alias="student")
pass


8 changes: 4 additions & 4 deletions tests/Generator/resource/python/namespace/my_map.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from pydantic import BaseModel, Field, GetCoreSchemaHandler
from pydantic import BaseModel, Field, GetCoreSchemaHandler, Tag
from pydantic_core import CoreSchema, core_schema
from typing import Any, Dict, Generic, List, Optional, TypeVar
from .student import Student
from typing import Any, Dict, Generic, List, Optional, TypeVar, Annotated, Union
from my.import import Student


class MyMap(My.Import.Student):
class MyMap(Student):
pass


4 changes: 2 additions & 2 deletions tests/Generator/resource/python/oop/human_map.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from pydantic import BaseModel, Field, GetCoreSchemaHandler
from pydantic import BaseModel, Field, GetCoreSchemaHandler, Tag
from pydantic_core import CoreSchema, core_schema
from typing import Any, Dict, Generic, List, Optional, TypeVar
from typing import Any, Dict, Generic, List, Optional, TypeVar, Annotated, Union
from .map import Map
from .human_type import HumanType

Expand Down
4 changes: 2 additions & 2 deletions tests/Generator/resource/python/oop/human_type.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from pydantic import BaseModel, Field, GetCoreSchemaHandler
from pydantic import BaseModel, Field, GetCoreSchemaHandler, Tag
from pydantic_core import CoreSchema, core_schema
from typing import Any, Dict, Generic, List, Optional, TypeVar
from typing import Any, Dict, Generic, List, Optional, TypeVar, Annotated, Union
from .human_type import HumanType


Expand Down
4 changes: 2 additions & 2 deletions tests/Generator/resource/python/oop/map.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from pydantic import BaseModel, Field, GetCoreSchemaHandler
from pydantic import BaseModel, Field, GetCoreSchemaHandler, Tag
from pydantic_core import CoreSchema, core_schema
from typing import Any, Dict, Generic, List, Optional, TypeVar
from typing import Any, Dict, Generic, List, Optional, TypeVar, Annotated, Union


P = TypeVar("P")
Expand Down
4 changes: 2 additions & 2 deletions tests/Generator/resource/python/oop/root_schema.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from pydantic import BaseModel, Field, GetCoreSchemaHandler
from pydantic import BaseModel, Field, GetCoreSchemaHandler, Tag
from pydantic_core import CoreSchema, core_schema
from typing import Any, Dict, Generic, List, Optional, TypeVar
from typing import Any, Dict, Generic, List, Optional, TypeVar, Annotated, Union
from .student_map import StudentMap


Expand Down
4 changes: 2 additions & 2 deletions tests/Generator/resource/python/oop/student.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from pydantic import BaseModel, Field, GetCoreSchemaHandler
from pydantic import BaseModel, Field, GetCoreSchemaHandler, Tag
from pydantic_core import CoreSchema, core_schema
from typing import Any, Dict, Generic, List, Optional, TypeVar
from typing import Any, Dict, Generic, List, Optional, TypeVar, Annotated, Union
from .human_type import HumanType


Expand Down
4 changes: 2 additions & 2 deletions tests/Generator/resource/python/oop/student_map.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from pydantic import BaseModel, Field, GetCoreSchemaHandler
from pydantic import BaseModel, Field, GetCoreSchemaHandler, Tag
from pydantic_core import CoreSchema, core_schema
from typing import Any, Dict, Generic, List, Optional, TypeVar
from typing import Any, Dict, Generic, List, Optional, TypeVar, Annotated, Union
from .map import Map
from .human_type import HumanType
from .student import Student
Expand Down

0 comments on commit 271a659

Please sign in to comment.