def __repr__(self) -> str:
return (
f"<Column {self.name!r},"
- f" type: {self._type_display()} (oid: {self.type_code})>"
+ f" type: {self.type_display} (oid: {self.type_code})>"
)
def __len__(self) -> int:
return 7
- def _type_display(self) -> str:
+ @property
+ def type_display(self) -> str:
+ """A pretty representation of the column type.
+
+ It is composed by the type name, followed by eventual modifiers and
+ brackets to signify arrays, e.g. :sql:`text`, :sql:`varchar(42)`,
+ :sql:`date[]`.
+ """
if not self._type:
return str(self.type_code)
- parts = []
- parts.append(self._type.name)
- mod = self._type.get_modifier(self._fmod)
- if mod:
- parts.append(f"({','.join(map(str, mod))})")
-
- if self.type_code == self._type.array_oid:
- parts.append("[]")
-
- return "".join(parts)
+ return self._type.get_type_display(oid=self.type_code, fmod=self._fmod)
def __getitem__(self, index: Any) -> Any:
if isinstance(index, slice):
@property
def display_size(self) -> Optional[int]:
- """The field size, for :sql:`varchar(n)`, None otherwise."""
+ """The field size, for string types such as :sql:`varchar(n)`."""
return self._type.get_display_size(self._fmod) if self._type else None
@property
"type, precision, scale, dsize, isize",
[
("text", None, None, None, None),
+ ("text[]", None, None, None, None),
("varchar", None, None, None, None),
("varchar(1)", None, None, 1, None),
+ ("varchar(1)[]", None, None, 1, None),
("varchar(42)", None, None, 42, None),
("bpchar(42)", None, None, 42, None),
("varchar(10485760)", None, None, 10485760, None),
("int4", None, None, None, 4),
("numeric", None, None, None, None),
("numeric(10,0)", 10, 0, None, None),
- ("numeric(10,3)", 10, 3, None, None),
+ ("numeric(10,3)[]", 10, 3, None, None),
("numeric(10,-1)", 10, -1, None, None),
("numeric(1,-1000)", 1, -1000, None, None),
("numeric(1,1000)", 1, 1000, None, None),
("numeric(1000,1000)", 1000, 1000, None, None),
("time", None, None, None, 8),
- ("time", None, None, None, 8),
+ ("time[]", None, None, None, None),
("timetz", None, None, None, 12),
("timestamp", None, None, None, 8),
("timestamptz", None, None, None, 8),
cur = conn.cursor()
cur.execute(f"select null::{type}")
col = cur.description[0]
- assert type in (repr(col))
+ assert type == col.type_display
+ assert f" {type} " in (repr(col))
assert col.precision == precision
assert col.scale == scale
assert col.display_size == dsize