Type hints - am I doing it right?
Thomas Passin
list1 at tompassin.net
Wed Dec 13 16:02:07 EST 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.
More information about the Python-list
mailing list