This project has moved and is read-only. For the latest updates, please go here.

Highlighting references

Oct 26, 2013 at 6:04 PM
Hi community,

is there a way to activate reference highlighting in PTVS as for other languages in VS?

For example:
from time import sleep

sleeep(1) // this is an unresolved reference and should be marked as error
Oct 26, 2013 at 7:37 PM
These are two different features.

One is what VS calls reference highlighting - highlighting other mentions of symbol under the cursor in the same file. This is certainly doable, as we already have all the necessary information for it, to drive Find All References. We just need to implement the corresponding VS API for highlighting. There is an open feature request for that in the tracker - please vote for it!

Marking "unresolved references" as errors is somewhat trickier. Because Python is a dynamic language, static code analysis cannot really determine that the identifier does not exist in a fully reliable way. Consider code like this:
globals()['a'] = 123
Note that there is no direct assignment to 'a' here - it's done through a dict - and yet print(a) is a valid reference. Now something like the above code can still be tracked, but the moment you start doing something more complicated, e.g. using computed values rather than literals to index globals(), it becomes much harder if not outright impossible.

Another problem is that Python doesn't really have definitions - it's all just assignments to variables, and those can be inside conditional blocks. E.g.:
if x:
  a = 1
  b = 1
print(a, b)
Neither 'a' nor 'b' are guaranteed to be defined by the time control flow reaches print - in fact, as written, this will always be a runtime error. Should they be marked as error because there's no guarantee? That would be a false positive on many common Python code patterns.

All in all, this is something that can certainly be done on a best-effort basis, but since it will always have some false positives due to the nature of the language, it's probably something that's best run on explicit request, rather than all the time in the background. I believe that the PyLint integration feature would cover that, once we implement it.