66from  __future__ import  annotations 
77
88from  dataclasses  import  dataclass 
9- from  typing  import  Any , ClassVar , Tuple 
9+ from  decimal  import  Decimal 
10+ from  typing  import  Any , ClassVar , Tuple , Union 
1011
1112from  pydantic  import  GetCoreSchemaHandler 
1213from  pydantic ._internal  import  _repr 
1314from  pydantic_core  import  ArgsKwargs , PydanticCustomError , core_schema 
1415
16+ LatitudeType  =  Union [float , Decimal ]
17+ LongitudeType  =  Union [float , Decimal ]
18+ CoordinateType  =  Tuple [LatitudeType , LongitudeType ]
19+ 
1520
1621class  Latitude (float ):
1722    """Latitude value should be between -90 and 90, inclusive. 
1823
24+     Supports both float and Decimal types. 
25+ 
1926    ```py 
27+     from decimal import Decimal 
2028    from pydantic import BaseModel 
2129    from pydantic_extra_types.coordinate import Latitude 
2230
@@ -25,9 +33,10 @@ class Location(BaseModel):
2533        latitude: Latitude 
2634
2735
28-     location = Location(latitude=41.40338) 
29-     print(location) 
30-     # > latitude=41.40338 
36+     # Using float 
37+     location1 = Location(latitude=41.40338) 
38+     # Using Decimal 
39+     location2 = Location(latitude=Decimal('41.40338')) 
3140    ``` 
3241    """ 
3342
@@ -36,13 +45,21 @@ class Location(BaseModel):
3645
3746    @classmethod  
3847    def  __get_pydantic_core_schema__ (cls , source : type [Any ], handler : GetCoreSchemaHandler ) ->  core_schema .CoreSchema :
39-         return  core_schema .float_schema (ge = cls .min , le = cls .max )
48+         return  core_schema .union_schema (
49+             [
50+                 core_schema .float_schema (ge = cls .min , le = cls .max ),
51+                 core_schema .decimal_schema (ge = Decimal (cls .min ), le = Decimal (cls .max )),
52+             ]
53+         )
4054
4155
4256class  Longitude (float ):
4357    """Longitude value should be between -180 and 180, inclusive. 
4458
59+     Supports both float and Decimal types. 
60+ 
4561    ```py 
62+     from decimal import Decimal 
4663    from pydantic import BaseModel 
4764
4865    from pydantic_extra_types.coordinate import Longitude 
@@ -52,9 +69,10 @@ class Location(BaseModel):
5269        longitude: Longitude 
5370
5471
55-     location = Location(longitude=2.17403) 
56-     print(location) 
57-     # > longitude=2.17403 
72+     # Using float 
73+     location1 = Location(longitude=2.17403) 
74+     # Using Decimal 
75+     location2 = Location(longitude=Decimal('2.17403')) 
5876    ``` 
5977    """ 
6078
@@ -63,7 +81,12 @@ class Location(BaseModel):
6381
6482    @classmethod  
6583    def  __get_pydantic_core_schema__ (cls , source : type [Any ], handler : GetCoreSchemaHandler ) ->  core_schema .CoreSchema :
66-         return  core_schema .float_schema (ge = cls .min , le = cls .max )
84+         return  core_schema .union_schema (
85+             [
86+                 core_schema .float_schema (ge = cls .min , le = cls .max ),
87+                 core_schema .decimal_schema (ge = Decimal (cls .min ), le = Decimal (cls .max )),
88+             ]
89+         )
6790
6891
6992@dataclass  
@@ -73,10 +96,11 @@ class Coordinate(_repr.Representation):
7396    You can use the `Coordinate` data type for storing coordinates. Coordinates can be 
7497    defined using one of the following formats: 
7598
76-     1. Tuple: `(Latitude, Longitude)`. For example: `(41.40338, 2.17403)`. 
99+     1. Tuple: `(Latitude, Longitude)`. For example: `(41.40338, 2.17403)` or `(Decimal('41.40338'), Decimal('2.17403'))` . 
77100    2. `Coordinate` instance: `Coordinate(latitude=Latitude, longitude=Longitude)`. 
78101
79102    ```py 
103+     from decimal import Decimal 
80104    from pydantic import BaseModel 
81105
82106    from pydantic_extra_types.coordinate import Coordinate 
@@ -86,7 +110,12 @@ class Location(BaseModel):
86110        coordinate: Coordinate 
87111
88112
89-     location = Location(coordinate=(41.40338, 2.17403)) 
113+     # Using float values 
114+     location1 = Location(coordinate=(41.40338, 2.17403)) 
115+     # > coordinate=Coordinate(latitude=41.40338, longitude=2.17403) 
116+ 
117+     # Using Decimal values 
118+     location2 = Location(coordinate=(Decimal('41.40338'), Decimal('2.17403'))) 
90119    # > coordinate=Coordinate(latitude=41.40338, longitude=2.17403) 
91120    ``` 
92121    """ 
@@ -102,7 +131,7 @@ def __get_pydantic_core_schema__(cls, source: type[Any], handler: GetCoreSchemaH
102131            core_schema .no_info_wrap_validator_function (cls ._parse_str , core_schema .str_schema ()),
103132            core_schema .no_info_wrap_validator_function (
104133                cls ._parse_tuple ,
105-                 handler .generate_schema (Tuple [ float ,  float ] ),
134+                 handler .generate_schema (CoordinateType ),
106135            ),
107136            handler (source ),
108137        ]
0 commit comments