Skip to content

Commit

Permalink
Use StrictFloat and StrictInt
Browse files Browse the repository at this point in the history
Use const=True for Field type_ for Schema.org model discrimination
  • Loading branch information
crbaker89 committed Feb 18, 2022
1 parent b954ba4 commit eecaa83
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"Date": ("date", f"datetime", "date"),
"DateTime": ("datetime", f"datetime", "datetime"),
"Time": ("time", "datetime", "time"),
"Number": ("int, float", "decimal", "Decimal"),
"Number": ("StrictInt, StrictFloat", "pydantic", "StrictInt, StrictFloat"),
"Float": ("float", "", ""),
"Integer": ("int", "", ""),
"Text": ("str", "", ""),
Expand Down
2 changes: 1 addition & 1 deletion src/templates/model.py.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class {{ model.valid_name }}({{model.parents| sort(attribute='depth', reverse=Tr
See: https://schema.org/{{ model.name }}
Model depth: {{model.depth}}
"""
type_: str = Field(default="{{ model.name }}", alias='@type', constant=True)
type_: str = Field(default="{{ model.name }}", alias='@type', const=True)
{% for field in model.fields -%}
{{ field.valid_name }}: {{ field.type }} = Field(
default=None,
Expand Down
4 changes: 2 additions & 2 deletions src/templates/schema_org_base.py.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ from datetime import time, datetime, date
from decimal import Decimal
from typing import Any, Optional, ForwardRef, List, Union

from pydantic import BaseModel, Field, StrictBool, AnyUrl
from pydantic import BaseModel, Field, StrictBool, AnyUrl, StrictInt, StrictFloat
from pydantic.typing import update_model_forward_refs

from pydantic_schemaorg.ISO8601.ISO8601Date import ISO8601Date
Expand Down Expand Up @@ -65,7 +65,7 @@ class SchemaOrgBase(BaseModel):
Try to update ForwardRefs on fields based on this Model, globalns and localns.
"""
locals = {'Optional': Optional, 'List': List, 'Union': Union, 'StrictBool': StrictBool, 'AnyUrl': AnyUrl,
'Decimal': Decimal, 'time': time, 'datetime': datetime, 'date': date,'ISO8601Date':ISO8601Date}
'Decimal': Decimal, 'time': time, 'datetime': datetime, 'date': date,'ISO8601Date':ISO8601Date, 'StrictInt':StrictInt, 'StrictFloat': StrictFloat}
for cls_ in cls.mro():
if hasattr(cls_, 'get_local_ns'):
locals.update(cls_.get_local_ns())
Expand Down

0 comments on commit eecaa83

Please sign in to comment.