Refactor parial types to avoid code duplication
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
Currently there is some duplication around partial types:
- Logic in
try_infer_partial_generic_type_from_assignment()essentially duplicates the logic inlined incheck_assignment()forNonepartial types. These two probably can be refactored to the same method if we also updatehandle_partial_var_type()to not special-case partialNonetypes, and instead consistently return a partial type in lvalue context. - Logic in
try_infer_partial_type_from_indexed_assignment()duplicates that intry_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 tocheck_call()), or by generating a syntheticCallExprwith__setitem__and passing it totry_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?
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by locating try_infer_partial_generic_type_from_assignment(), check_assignment(), handle_partial_var_type(), try_infer_partial_type_from_indexed_assignment(), and try_infer_partial_type(). Compare their partial-type handling, then evaluate the defaultdict and setdefault examples as regression cases; done means the duplicated logic is unified without breaking those inference scenarios.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- compilers, devtools
- Issue type
- Refactor
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100