Skip to content

mypy is unhappy with repeated use of _ as a throw-away variable #1332

Closed

Description

@ddfisher

_ is commonly used as a throw-away variable in list unpacking, but this will make mypy unhappy if two lists with different types are unpacked in the same function. For example

x, _ = ["foo", "bar"]
n, _ = [1, 2]

results in:

test.py:2: error: Incompatible types in assignment (expression has type "int", variable has type "str")

To support this pattern, we may want to consider special-casing _ and not generating this sort of type error if the variable is never read.

Activity

  1. gvanrossum commented on Apr 5, 2016

    @gvanrossum
    Member

    A complication is that _ is also commonly used as an alias for gettext. When we see an import like from ... import gettext as _ we should not special-case it (and probably complain extra loudly if _ is redefined locally).

  2. JukkaL commented on Apr 5, 2016

    @JukkaL
    Collaborator

    In the gettext use case _ it will be accessed within the module. As @ddfisher suggested, perhaps we should only special case _ if it's never read.

  3. added this to the milestone on Apr 7, 2016
  4. dmoisset commented on Jun 9, 2016

    @dmoisset
    Contributor

    In my opinion, any variable (no matter its name) which is used only write only can be safely typed to Any (or to object), given that this can never be an unsafe operation, so it's sound and cover this pattern well (and some alternative things like using __ as placeholder)

  5. JukkaL commented on Jun 9, 2016

    @JukkaL
    Collaborator

    At module and class top levels we can't determine whether a variable will be read outside the module/class, so at least here a naming convention helps.

  6. dmoisset commented on Jun 9, 2016

    @dmoisset
    Contributor

    Yes, my comment was made thinking about locals

  7. dmoisset commented on Jun 10, 2016

    @dmoisset
    Contributor

    A note that may help clean up the issue tracker: this seems to be the same issue as #465 and #1649, they can probably be unified

  8. ddfisher commented on Jun 10, 2016

    @ddfisher
    CollaboratorAuthor

    Instead of special casing write-only variables to have the Any type, we could also just be willing to infer Union types for things. E.g.:

    _ = 1
    _ = "foo"
    # _ now has type Union[int, str]

    Come to think of it, this would likely also solve the "variable must only have a single type" problem (provided that is something we want to solve) because of the way we already understand assignments to Union types.

  9. gvanrossum commented on Jun 10, 2016

    @gvanrossum
    Member
  10. ddfisher commented on Jun 11, 2016

    @ddfisher
    CollaboratorAuthor

    Not necessarily! We add more specific types to the binder on assignment, so e.g.:

    from typing import Union
    
    x = 0  # type: Union[str, int]
    x = "foo"
    reveal_type(x)  # Revealed type is 'builtins.str'
    x = 0
    reveal_type(x)  # Revealed type is 'builtins.int'
  11. JukkaL commented on Mar 3, 2017

    @JukkaL
    Collaborator

    Closing this as a duplicate of #465.

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