Skip to content

Underspecified type variables should not infer UninhabitedType #3032

Open

Description

@ddfisher

Unconstrained TypeVars should not infer UninhabitedType as the result -- it leads to confusing behavior.

(See: testUnderspecifiedInferenceResult)

Activity

  1. pkch commented on Apr 14, 2017

    @pkch
    Contributor

    (Sorry, heavily edited... it's more complex than I thought.)

    AFAIU, the rule for inferring the type of lvalue in an assignment is intended to be roughly this:

    1. Use the knowledge of lvalue type to obtain constraints on all the type variables in rvalue type.
    2. Within those constraints, find concrete types for the type variables that result in the lowest possible final type. Replace type variables with those concrete types if not <uninhabited>.
    3. If a concrete type found for a type variable is <uninhabited>, but the constraint is not equality, leave the type variable unsubstituted, so it becomes a type parameter in the final result
    4. If any <uninhabited> remains, mark the code as unreachable.

    I think the problem is that mypy sometimes ends up not doing step 3, instead leaving <uninhabited> in the inferred type. This leads to confusing messages and incorrect tagging of code as unreachable.

    That said, I don't completely understand step 2, since the "lowest" type may not exist; but I hope the rest of my comments are still correct.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions