1"""
2Wrappers for numeric types.
3"""
4
5# Copyright (C) 2020-2021 The Psycopg Team
6
7# Wrappers to force numbers to be cast as specific PostgreSQL types
8
9# These types are implemented here but exposed by `psycopg.types.numeric`.
10# They are defined here to avoid a circular import.
11_MODULE = "psycopg.types.numeric"
12
13
14class Int2(int):
15    """
16    Force dumping a Python `!int` as a PostgreSQL :sql:`smallint/int2`.
17    """
18
19    __module__ = _MODULE
20    __slots__ = ()
21
22    def __new__(cls, arg: int) -> "Int2":
23        return super().__new__(cls, arg)
24
25    def __str__(self) -> str:
26        return super().__repr__()
27
28    def __repr__(self) -> str:
29        return f"{self.__class__.__name__}({super().__repr__()})"
30
31
32class Int4(int):
33    """
34    Force dumping a Python `!int` as a PostgreSQL :sql:`integer/int4`.
35    """
36
37    __module__ = _MODULE
38    __slots__ = ()
39
40    def __new__(cls, arg: int) -> "Int4":
41        return super().__new__(cls, arg)
42
43    def __str__(self) -> str:
44        return super().__repr__()
45
46    def __repr__(self) -> str:
47        return f"{self.__class__.__name__}({super().__repr__()})"
48
49
50class Int8(int):
51    """
52    Force dumping a Python `!int` as a PostgreSQL :sql:`bigint/int8`.
53    """
54
55    __module__ = _MODULE
56    __slots__ = ()
57
58    def __new__(cls, arg: int) -> "Int8":
59        return super().__new__(cls, arg)
60
61    def __str__(self) -> str:
62        return super().__repr__()
63
64    def __repr__(self) -> str:
65        return f"{self.__class__.__name__}({super().__repr__()})"
66
67
68class IntNumeric(int):
69    """
70    Force dumping a Python `!int` as a PostgreSQL :sql:`numeric/decimal`.
71    """
72
73    __module__ = _MODULE
74    __slots__ = ()
75
76    def __new__(cls, arg: int) -> "IntNumeric":
77        return super().__new__(cls, arg)
78
79    def __str__(self) -> str:
80        return super().__repr__()
81
82    def __repr__(self) -> str:
83        return f"{self.__class__.__name__}({super().__repr__()})"
84
85
86class Float4(float):
87    """
88    Force dumping a Python `!float` as a PostgreSQL :sql:`float4/real`.
89    """
90
91    __module__ = _MODULE
92    __slots__ = ()
93
94    def __new__(cls, arg: float) -> "Float4":
95        return super().__new__(cls, arg)
96
97    def __str__(self) -> str:
98        return super().__repr__()
99
100    def __repr__(self) -> str:
101        return f"{self.__class__.__name__}({super().__repr__()})"
102
103
104class Float8(float):
105    """
106    Force dumping a Python `!float` as a PostgreSQL :sql:`float8/double precision`.
107    """
108
109    __module__ = _MODULE
110    __slots__ = ()
111
112    def __new__(cls, arg: float) -> "Float8":
113        return super().__new__(cls, arg)
114
115    def __str__(self) -> str:
116        return super().__repr__()
117
118    def __repr__(self) -> str:
119        return f"{self.__class__.__name__}({super().__repr__()})"
120
121
122class Oid(int):
123    """
124    Force dumping a Python `!int` as a PostgreSQL :sql:`oid`.
125    """
126
127    __module__ = _MODULE
128    __slots__ = ()
129
130    def __new__(cls, arg: int) -> "Oid":
131        return super().__new__(cls, arg)
132
133    def __str__(self) -> str:
134        return super().__repr__()
135
136    def __repr__(self) -> str:
137        return f"{self.__class__.__name__}({super().__repr__()})"
138