Re: Type hints - am I doing it right?
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. -- https://mail.python.org/mailman/listinfo/python-list
Re: Type hints - am I doing it right?
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. -- https://mail.python.org/mailman/listinfo/python-list
Re: Type hints - am I doing it right?
On 13Dec2023 09:19, Frank Millman 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 -- https://mail.python.org/mailman/listinfo/python-list
Type hints - am I doing it right?
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 -- https://mail.python.org/mailman/listinfo/python-list