Skip to content

Support ipaddress objects as parameters (#43) - #118

Merged
Cito merged 3 commits into
mainfrom
network-types-input
Nov 22, 2026
Merged

Cito merged 3 commits into
mainfrom
network-types-input

Conversation

@Cito

@Cito Cito commented Nov 22, 2026

Copy link
Copy Markdown
Member

Objects of the ipaddress module could only be passed as separate parameters in pg, where they were converted to strings. As inline parameters in pg and as parameters in pgdb, they raised an InterfaceError. This adapts them in all paths, as in psycopg 3, and adds opt-in typecast functions for returning them. Closes #43.

Main changes:

  • Addresses and interfaces are adapted as inet, networks as cidr, in pg (format_query() and query_formatted() with inline=True) and in pgdb, also as elements of arrays and records. The values are escaped, since the scope id of an IPv6 address may contain quotes.
  • Arrays of types unknown to PyGreSQL (e.g. inet, cidr or enums) got the simple type text instead of text[], so DB.insert() and DB.update() sent the Python list as a string. They now get text[], also as type names like _inet. This changes what DB.get_attnames() returns for such columns.
  • New typecast functions cast_inet() and cast_cidr() in pg.cast and pgdb.cast. They are not used by default, so inet and cidr values are still returned as strings, but can be set with set_typecast(). Making them the default would be a breaking change, which I will propose separately for 7.0.

Cito added 3 commits November 22, 2026 15:53
Arrays of types that PyGreSQL does not know, e.g. of enums or of inet,
got the simple type "text" instead of "text[]", so DB.insert() and
DB.update() sent the Python list as a string. Such array types now get
the simple type "text[]", and type names like "_inet" passed to
format_query() are recognized as arrays as well. Types that are only
named like arrays, but are not arrays, are still mapped to "text".
Objects of the ipaddress module were only adapted when passed as separate
parameters in pg, where they were converted to strings. As inline
parameters in pg and as parameters in pgdb, they raised an InterfaceError.
Now addresses and interfaces are adapted as inet and networks as cidr,
as in psycopg 3, also as elements of arrays and records. The values are
escaped, since the scope id of an IPv6 address may contain quotes.
The new functions cast_inet() and cast_cidr() in pg.cast, also available
in pgdb.cast, cast inet and cidr values to objects of the ipaddress
module, as in psycopg 3: inet values with a prefix length to interfaces,
other values to addresses, and cidr values to networks. They are not
used by default, since these values are still returned as strings, but
can be set with set_typecast().
@Cito
Cito merged commit 7e1a4c8 into main Nov 22, 2026
22 checks passed
@Cito
Cito deleted the network-types-input branch November 22, 2026 16:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support networking data types

1 participant