• Type hints - am I doing it right?

    From Frank Millman@21:1/5 to All on Wed Dec 13 09:19:16 2023
    Hi all

    I am adding type hints to my code base.

    I support three databases - sqlite3, Sql Server, PostgreSQL. The db
    parameters are kept in an ini file, under the section name 'DbParams'.
    This is read on program start, using configparser, and passed to a
    function config_database() in another module with the argument
    cfg['DbParams'].

    In the other module I have this -

         def config_database(db_params):

    To add a type hint, I now have this -

         def config_database(db_params: configparser.SectionProxy):

    To get this to work, I have to add 'import configparser' at the top of
    the module.

    I have three separate modules, one for each database, with a subclass containing the methods and attributes specific to that database. Each
    one has a connect() method which receives db_params as a parameter. Now
    I have to add 'import configparser' at the top of each of these modules
    in order to type hint the method.

    This seems verbose. If it is the correct way of doing it I can live with
    it, but I wondered if there was an easier way.

    BTW I have realised that I can bypass the problem by converting
    db_params to a dict, using dict(cfg['DbParams']). But I would still like
    an answer to the original question, as I am sure similar situations will
    occur without such a simple solution.

    Thanks

    Frank Millman

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Cameron Simpson@21:1/5 to Frank Millman on Wed Dec 13 19:47:00 2023
    On 13Dec2023 09:19, Frank Millman <frank@chagford.com> wrote:
    I am adding type hints to my code base.
    [...]
    In the other module I have this -

         def config_database(db_params):

    To add a type hint, I now have this -

         def config_database(db_params: configparser.SectionProxy):

    To get this to work, I have to add 'import configparser' at the top of
    the module.

    I have three separate modules, one for each database, with a subclass >containing the methods and attributes specific to that database. Each
    one has a connect() method which receives db_params as a parameter.
    Now I have to add 'import configparser' at the top of each of these
    modules in order to type hint the method.

    This seems verbose. If it is the correct way of doing it I can live
    with it, but I wondered if there was an easier way.

    Not really. It's like any other name - it needs importing if you're
    going to use it.

    You can make the hint itself more compact:

    from configparser import SectionProxy
    .......
    def config_database(db_params: SectionProxy):

    Or you could be a bit more agnostic:

    from typing import Mapping
    .......
    def config_database(db_params: Mapping):

    since I imagine config_database() would accept any kind of mapping
    (dicts, etc etc).

    Cheers,
    Cameron Simpson <cs@cskk.id.au>

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Mats Wichmann@21:1/5 to Frank Millman via Python-list on Wed Dec 13 09:17:40 2023
    On 12/13/23 00:19, Frank Millman via Python-list wrote:

    I have to add 'import configparser' at the top of each of these modules
    in order to type hint the method.

    This seems verbose. If it is the correct way of doing it I can live with
    it, but I wondered if there was an easier way.

    Think of import as meaning "make this available in my current (module) namespace".

    The actual import machinery only runs the first time, that is, if it's
    not already present in the sys.modules dict.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Thomas Passin@21:1/5 to Mats Wichmann via Python-list on Wed Dec 13 16:02:07 2023
    On 12/13/2023 11:17 AM, Mats Wichmann via Python-list wrote:
    On 12/13/23 00:19, Frank Millman via Python-list wrote:

    I have to add 'import configparser' at the top of each of these
    modules in order to type hint the method.

    This seems verbose. If it is the correct way of doing it I can live
    with it, but I wondered if there was an easier way.

    Think of import as meaning "make this available in my current (module) namespace".

    The actual import machinery only runs the first time, that is, if it's
    not already present in the sys.modules dict.

    There's also the approach of importing the typing objects conditionally,
    as in this snippet from the Leo Editor (https://github.com/leo-editor/leo-editor)

    if TYPE_CHECKING: # pragma: no cover
    from leo.core.leoCommands import Commands as Cmdr
    from leo.core.leoGui import LeoKeyEvent as Event

    Yes, it's more verbose but it makes clear what the intent is.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)