Skip to content

Fixes to tuple fallbacks #6442

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

Merged
merged 17 commits into from
Feb 22, 2019
Merged

Fixes to tuple fallbacks #6442

merged 17 commits into from
Feb 22, 2019

Conversation

JukkaL
Copy link
Collaborator

@JukkaL JukkaL commented Feb 20, 2019

This changes tuple fallbacks to be calculated on demand during
type checking. This fixes some issues with fallbacks being imprecise.

In the new semantic analyzer, this introduces a new pass just after
the main semantic analysis pass to calculate precise item types for
tuple base classes. These can't be calculated on demand since
the base class is a variable-length tuple type (Instance) instead
of TupleType. We can't calculate these during the main semantic
analysis pass since base classes can be incomplete.

Fixes #6400.

@JukkaL JukkaL requested a review from ilevkivskyi February 20, 2019 12:04
Copy link
Member

@ilevkivskyi ilevkivskyi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! This is a large piece of work. I have few comments and suggestions.

@@ -2785,7 +2787,7 @@ def visit_tuple_expr(self, e: TupleExpr) -> Type:
tt = self.accept(item, type_context_items[j])
j += 1
items.append(tt)
fallback_item = join.join_type_list(items)
fallback_item = AnyType(TypeOfAny.special_form)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add a comment above reminding that this is just a partial fallback, and a real fallback is always calculated on-demand.

@@ -19,12 +20,8 @@
# Priorities for ordering of patches within the final "patch" phase of semantic analysis
# (after pass 3):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no pass three anymore, so this comment can also be updated.


This must be called only after the main semantic analysis pass, since joins
aren't available before that.
"""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would mention the chicken and egg problem about tuple fallbacks and checking upper bounds for instances here, and/or maybe in a comment in semanal_main() where the two are called.

class C(A): pass

class T(Tuple[B, C]):
pass
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe put the classes in reverse order (just to check that forward refs also work, although I am quite they should).


class NTStr(NamedTuple):
x: str
y: str
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would add couple non-trivial upper bound checks, just be sure they behave reasonably with the current ordering (fallbacks first, then bounds).

for nx in nt:
reveal_type(nx) # E: Revealed type is 'Union[builtins.int*, builtins.str*]'

t: Union[Tuple[int, int], Tuple[str, str]]
for x in t:
# TODO(Ivan): This will be OK when tuple fallback patches are added (like above)
reveal_type(x) # E: Revealed type is 'Any'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is also an issue about this on tracker #3575. Also there is a chance that #5393 will be fixed by this PR. Should we close issues when they are fixed in the new analyzer?

@@ -204,6 +217,11 @@ def semantic_analyze_target(target: str,
return [], analyzer.incomplete


def process_patches(graph: 'Graph', scc: List[str]) -> None:
for module in scc:
graph[module].semantic_analysis_apply_patches()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would add a motivational docstring explaining why we do mini-passes for one things, and patches for other. My understanding that there will be quite few tuple types in a typical program, but quite a lot instances, so we a make a traverser for the latter.

Also there is an option to combine these two in a single type fixing traverser pass, and then repeat it until we solved the chicken and egg problem (this would require to somehow mark types as ready). I however didn't think much about this, maybe this will not help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

New semantic analyzer: calculating fallback types
2 participants