Skip to content

WIP: Various typing fixes for the AST

FeRD (Frank Dana) requested to merge ferdnyc/gi-docgen:typing-fixes into main

Some initial tweaks to ast.py, to address the sort of typing issues that make mypy lose its cool.

Most should (hopefully) be uncontroversial, so far. Things like...

typing.Mapping being used as a concrete type

Mapping is an abstract type, useful for signatures. But a dict variable isn't a Mapping, it's a Dict. (Just like a tuple or a list isn't a Sequence, though they are both Sequence types.)

# This
self._somethings: T.Mapping[str, Something] = {}
# Becomes this
self._somethings: T.Dict[str, Something] = {}

Protecting against property accesses through None

One of the most frustrating things about typing (but also why it's useful): If you say your property can be None, then you can't access any of its properties without checking first, and you can't use it in any contexts where None isn't permitted.

# So, things like...
for foo in things_which_can_be_or_have_None:
    some_dict[foo.name] = foo
# Have to be...
some_dict.update({
    foo.name: foo
    for foo in things_which_can_be_or_have_None
    if hasattr(foo, 'name') and foo.name is not None
})

Other changes

A couple do have logic implications (super().__contains__(super, foo) being called, which should just be super().__contains__(foo) as the self arg is supplied by super()), but... they weren't right before.

Merge request reports