Python Forum
Typing and variable initializers
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Typing and variable initializers
#1
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?".
Reply


Messages In This Thread
Typing and variable initializers - by jgossage - May-29-2020, 03:18 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Pywinauto typing in the wrong field EGameiro 0 690 Jun-07-2023, 10:01 PM
Last Post: EGameiro
  Mypy typing error WannaBePythonDev 2 1,940 Sep-05-2021, 10:18 AM
Last Post: WannaBePythonDev
  Need Help Typing Text into Tough Form [xpath / selenium] digitalmatic7 0 1,767 Jun-05-2019, 06:46 AM
Last Post: digitalmatic7
  Is there any way to simulate clicks/typing to an inactive window using Python? Nwb 1 5,940 Jun-11-2018, 08:56 AM
Last Post: Larz60+
  Typing into CMD Prompt while its behind another window andy410 0 2,541 Apr-27-2018, 08:24 PM
Last Post: andy410

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020