Skip to content

Refactor parial types to avoid code duplication #8043

Open
@ilevkivskyi

Description

@ilevkivskyi

Currently there is some duplication around partial types:

  • Logic in try_infer_partial_generic_type_from_assignment() essentially duplicates the logic inlined in check_assignment() for None partial types. These two probably can be refactored to the same method if we also update handle_partial_var_type() to not special-case partial None types, and instead consistently return a partial type in lvalue context.
  • Logic in try_infer_partial_type_from_indexed_assignment() duplicates that in try_infer_partial_type() (there is already a TODO item). This one can be refactored by either pushing the latter a bit down the call stack (closer to check_call()), or by generating a synthetic CallExpr with __setitem__ and passing it to try_infer_partial_type().

The second item may be something to watch out when implementing support for these:

a = defaultdict(list)
a[0].append('yes')

b = {}
b.setdefault(0, []).append('yes')

c = defaultdict(set)
c[0].add('yes')

d = {}
d.setdefault(0, set()).add('yes')

@JukkaL this is probably not something important, but maybe it makes sense to fix this while we are at it?

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions