[ty] Improve semantic token classification for names by MichaReiser · Pull Request #21399 · astral-sh/ruff (original) (raw)

Summary

This PR addresses the first TODO in astral-sh/ty#791

Improve classification for name tokens that are imported from other modules. Currently, these are classified based on their types, which often means they're classified as variables when they should be classes, etc.

The semantic token provider was written before we added the more involved definitions_for_name that resolves definitions across modules. This PR changes the semantic token provider to use definitions_for_name instead of rolling its own, very limited definition lookup (that only performed a lookup in the same scope).

This should fix many miss classifications, like that float was classified as a Variable instead of a Class.

This PR further fixes some more misclasification:

Test plan

Added tests, I compared all classification changes with what Pylance does