Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kfmwi-0005hw-Os for psycopg@arkaria.postgresql.org; Thu, 19 Nov 2020 16:38:08 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kfmwh-0001K5-Jb for psycopg@arkaria.postgresql.org; Thu, 19 Nov 2020 16:38:07 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kfmwh-0001Jy-Ds for psycopg@lists.postgresql.org; Thu, 19 Nov 2020 16:38:07 +0000 Received: from mail-ot1-x332.google.com ([2607:f8b0:4864:20::332]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1kfmwd-0005i0-Og for psycopg@postgresql.org; Thu, 19 Nov 2020 16:38:06 +0000 Received: by mail-ot1-x332.google.com with SMTP id f16so5858408otl.11 for ; Thu, 19 Nov 2020 08:38:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=kWPEGWGhPKdMBG5BdmcbMXp/R3Oj1y+c0cFENnifg7Q=; b=kg5i/7oucjOMzV1EvLCb9EOTLxjmkxbppqs0GUPUcGrEpihnSakNvIHOkKQ4n8Ujr7 CCKf5G3AXrBoa0kV438rT3A6Cj5pAagstJDPAUl0Myo4lny7Yi8/pxKxIgPMAcek7yho Ct7wzaYRbr7fsJuXmglIkuU9JiGVFbgCvezx04HK++UVab+u7NPxZyqX4jjPZumhTaCH tBWzYtrFlC6GCKjoYofUIm8EiBBBkjYs/iiqgjxXn5J0ZqNK69A/44efxexunMkMJz7h jos/ABs5+EvNzW8Gdry4rNpD+l+aD6rgXd7k7C5phx5V9EpJSGwdI3oZjfT/Htb6p/BD IJBQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=kWPEGWGhPKdMBG5BdmcbMXp/R3Oj1y+c0cFENnifg7Q=; b=WkYridu0FsJ1Dh61xhp8FByanwPvq5NjKUIpPLe7nvXcT/8DbMKDYBqWCQPZ1ugjGk GRNDIF5ZQ9/xwYuKGYYFtG0P2hNGI+fvdRg/WXS4/QCQXyOD1x6g9v1uKj8QuThcOr1v 87kh4LU43lYD46wX0NKQZYQMbkmI0oW4ULQmJrEHm259dJXfEVeMZWxiI12XSE9m6b37 PyLOD3TbyJLSCZ5v80i1LQawhesi54PuiArAhnHCGlXATqdMUFDFFn5FMxyYw21cwELF Yjd+qKalskEkBQIo+QVAVBLGWoiI+3M6cRkk/ZpxGhAmqRz0pA2E5v1fVwLXgL0nQoju wTcQ== X-Gm-Message-State: AOAM533m+OHlGsU0kr53nqzqDqW/nj+eXUAt26f9YxjGWWcdCkx8BBpB 9Zul6pomLkI2wvG3zjB+z8BsiyEVv/3hYes+ddE= X-Google-Smtp-Source: ABdhPJySwo2hpMe6vHEzlIDkf5i22jH0DL5zZauZjvywqoeE3eKQRCgh9iE92wb0ZIgIK/nZYsgLTvW/qQo67x4TjN4= X-Received: by 2002:a9d:460a:: with SMTP id y10mr9820567ote.99.1605803882842; Thu, 19 Nov 2020 08:38:02 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: From: Daniele Varrazzo Date: Thu, 19 Nov 2020 16:37:51 +0000 Message-ID: Subject: Re: 'Psycopg2.errors' not referenced in packaged To: Patrick Starrenburg Cc: psycopg@postgresql.org Content-Type: text/plain; charset="UTF-8" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Thu, 19 Nov 2020 at 15:42, Patrick Starrenburg wrote: > I am using psycopg2 in a Django app. In my code I am checking for various exceptions, one of which is foreign key violations. > > I see in the psycopg2 docs https://www.psycopg.org/docs/errors.html that ForeignKeyViolation > is under the base IntegrityError exception. I would like to be able to specifically catch, and respond to the ForeignKeyViolation error. I am coding using the recommendation in the docs (below). > > It is working (a forced FK violation is specifically caught) my question is though that in my IDE (PyCharm) it reports that it cannot find the reference 'errors' and I cannot use dotted links to drill down to the errors.ForeignKeyViolation function. > > I can type psycopg2.IntegrityError and reference to it is found (and I see that it is listed in the __init.py__ file). > > When I Google this some say it is a PyCharm bug, some say that PyCharm is following the Python package convention. Yes, it is a PyCharm shortcoming: the exception is there: >>> import psycopg2.errors >>> psycopg2.errors.ForeignKeyViolation psycopg2.errors.ForeignKeyViolation The exceptions are defined in the C module, but they are exposed in the errors module. If you look at the source code for the module, it appears mostly empty: the exceptions are added there when the psycopg2._psycopg internal module is imported. Personally this is good enough: the module works as expected for all Python code; supporting PyCharm is not mandatory as far as I'm concerned. I am happy to receive patches to make PyCharm work better, but working on it myself isn't my priority now. -- Daniele