Skip to content

Incorrect Decimal('Infinity') adaptation #1724

@hordiienko-v

Description

@hordiienko-v
  • OS: Ubuntu
  • Psycopg version: 2.9.9
  • Python version: 3.9
  • PostgreSQL version: 16.2
  • pip version: 21.2.4

Since v14, PostgreSQL NUMERIC type supports Infinity value. But for now, psycopg2 adapts Infinity as 'NaN'::numeric. This can be avoided by creating custom type adapter that wraps decimal value in quotes, but still, adapting to NaN seems like outdated logic.

from decimal import Decimal

from psycopg2 import connect
from psycopg2._psycopg import connection, cursor

if __name__ == "__main__":
    conn: connection
    curr: cursor

    with connect("dbname=test_db user=test_user password=test_user") as conn:
        with conn.cursor() as curr:
            q = curr.mogrify(
                "INSERT INTO test VALUES (%s), (%s)",
                [Decimal("1"), Decimal("Infinity")],
            )
            print(q)
            # b"INSERT INTO test VALUES (1), ('NaN'::numeric)"

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions