You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Give a warning if a local variable is assigned to but never read #76
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.
@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".
@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 unusedx=456
This, I believe is similar in nature, but gives a type error instead:
x=123# value is still unused herex="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=123print(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.
Activity
sPredictorX1708 commentedon Feb 23, 2020
@JukkaL Do you mean something like this:
,in this y maybe misspelled local variable which is assigned to but never read.
JukkaL commentedon Feb 28, 2020
@dubesar Yeah! Or something like this:
sudhirkumar43 commentedon Mar 14, 2020
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.
JukkaL commentedon Mar 20, 2020
@sudhirkumar43 Can you rephrase your question? I wasn't quite sure what you mean exactly.
sudhirkumar43 commentedon Mar 20, 2020
@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".
JukkaL commentedon Mar 20, 2020
It would be sufficient to give a message such as
Value assigned to "processsor" is never read.sudhirkumar43 commentedon Mar 20, 2020
@JukkaL
But here is a different case processsor is never initialized and then how can we use it?
dosisod commentedon Oct 8, 2022
@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:
This, I believe is similar in nature, but gives a type error instead:
Adding
--allow-redefinitionfixes this, but only if you read fromxfirst:A few thoughts:
Do you think it is too strict of a requirement that
xmust 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_redefinitionwill make a newxvariable wheneverxis 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.