Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor parial types to avoid code duplication #8043

Open
ilevkivskyi opened this issue Nov 30, 2019 · 1 comment
Open

Refactor parial types to avoid code duplication #8043

ilevkivskyi opened this issue Nov 30, 2019 · 1 comment
Labels
priority-1-normal refactoring Changing mypy's internals

Comments

@ilevkivskyi
Copy link
Member

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?

@ilevkivskyi ilevkivskyi added refactoring Changing mypy's internals priority-1-normal labels Nov 30, 2019
@JukkaL
Copy link
Collaborator

JukkaL commented Dec 2, 2019

These refactorings sounds like good ideas. I'd prefer not creating temporary node objects if possible, as they tend to increase complexity and sometimes cause trouble with error location reporting.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
priority-1-normal refactoring Changing mypy's internals
Projects
None yet
Development

No branches or pull requests

2 participants