Python Forum

Full Version: Typing and variable initializers
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am starting to use typing and have come up with problems working out how to use typing in a particular situation.
I am running Python 3.8.3 in Eclipse 2020-03 with the Pydev plugin 7.5.

I give some of my attempts:

x = {}
I want to use static typing with this code. The following test code shows some of my attempts to discover how to do this:
# Ignore the reported error here. Irrelevant to this discussion.
from typing import Any, Mapping

# This matches what I expect the dictionary to contain except that the type of
# the dictionary initializer, keyed 'mno', is not defined.
d: Mapping[str, Any] = {'abc': None, 'def': 'ghi', 'jkl': 1,
                        'mno': {'xyz': 'abc'}}
# I know this is invalid but I am looking for a way to specify the type of
# an initializer when it is not determinable from the code.
e: Mapping[str, Any] = {'abc': None, 'def ': 'ghi', 'jkl': 1,
                        'mno': Mapping[str, str] {'xyz': 'abc'}}
# Here I want to use an empty dictionary as an initializer and my pseudo code
# to describe the type of the initializer is accepted.
f: Mapping[str, str] = {'abc': None, 'def': 'ghi', 'jkl': 1,
                        'mno': Mapping[str, str] {}}
# I have been unable to find any documentation describing how to specify the
# type of an initializer, so my question is "how is it supposed to be done?".