Skip to content

Give a warning if a local variable is assigned to but never read #76

Open

Description

@JukkaL

The type checker should give a warning if a local variable is only assigned to but never read.
This is usually a result of a misspelled variable name.

Perhaps only do this if warnings have been explicitly enabled using a command line option or via some other mechanism.

Activity

  1. sPredictorX1708 commented on Feb 23, 2020

    @sPredictorX1708

    @JukkaL Do you mean something like this:

    def foo(x:int) -> int:
        x = 5
        y = 5
        return x

    ,in this y maybe misspelled local variable which is assigned to but never read.

  2. JukkaL commented on Feb 28, 2020

    @JukkaL
    CollaboratorAuthor

    @dubesar Yeah! Or something like this:

    def thing() -> str:
        processor = 'default'
        if cond():
            processsor = 'extra'  # Note misspelling -- extra 's'
        return processor
  3. sudhirkumar43 commented on Mar 14, 2020

    @sudhirkumar43

    Hi @JukkaL,
    Its like point the error like processsor is not defined earlier? Since we don't know whether the two words are pointing the same variable or different.

  4. JukkaL commented on Mar 20, 2020

    @JukkaL
    CollaboratorAuthor

    @sudhirkumar43 Can you rephrase your question? I wasn't quite sure what you mean exactly.

  5. sudhirkumar43 commented on Mar 20, 2020

    @sudhirkumar43

    @JukkaL
    From the above Example:-
    def thing() -> str:
    processor = 'default'
    if cond():
    processsor = 'extra' # Note misspelling -- extra 's'
    return processor
    here should it raise error like processsor is not defined earlier or we should print the error like the spelling error? Since we don't know whether the two words (processsor , processor ) are pointing the same variable or different. It may possible that processsor is the name of other variable. I think we can do one thing that just check if a word is similar to some predefined word then raise an error. for example, processsor is much similar to processor raise the error "either missplled word or variable is not defined earlier".

  6. JukkaL commented on Mar 20, 2020

    @JukkaL
    CollaboratorAuthor

    It would be sufficient to give a message such as Value assigned to "processsor" is never read.

  7. sudhirkumar43 commented on Mar 20, 2020

    @sudhirkumar43

    @JukkaL
    But here is a different case processsor is never initialized and then how can we use it?

  8. dosisod commented on Oct 8, 2022

    @dosisod
    Contributor

    @JukkaL, I have been dealing with a similar issue with Refurb (see comment), curious what your thoughts are on the following:

    This currently passes in Mypy, and this issue proposes that a warning should be emitted:

    x = 123  # WARN: value unused
    x = 456

    This, I believe is similar in nature, but gives a type error instead:

    x = 123    # value is still unused here
    x = "abc"  # but you get a type error here
    main.py:2: error: Incompatible types in assignment (expression has type "str", variable has type "int")
    

    Adding --allow-redefinition fixes this, but only if you read from x first:

    x = 123
    print(x)
    x = "abc"

    A few thoughts:

    • Do you think it is too strict of a requirement that x must be read first before a new variable is created? I feel like this behaviour gives you an inconvinent type error, when in reality it should be telling you that the value you used is never read (at least in this instance that is).

    • Wouldn't it be more advantageous that enabling allow_redefinition will make a new x variable whenever x is assigned, regardless of whether it has been read or not? If you kept the ref count, you would be able to tell pretty easily if the value had been read or not. This would bring us closer to how SSA works in say LLVM, as opposed to only creating a new variable when the type changes.

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