snok / flake8-type-checking

Flake8 plugin for managing type-checking imports & forward references.
BSD 3-Clause "New" or "Revised" License
113 stars 16 forks source link

Special case `Annotated` when walking annotation expressions #192

Open Daverball opened 3 weeks ago

Daverball commented 3 weeks ago

Currently we treat Annotated like any other annotation, but it probably makes sense to at the very least treat the second item in the subscript argument differently, since it is intended to encapsulate arbitrary values, which will likely be accessed at runtime.

So we should treat Annotated itself and the entire expression of the second item as a soft-runtime use. I'm torn about whether we should treat the first item as a soft-runtime use as well. On the one hand it will be necessary to make typing.get_type_hints work, on the other hand, there's less of a reason to use typing.get_type_hints when only trying to retrieve the second item of Annotated. Maybe this should be a setting?

One big caveat of this special casing is, that we may not have a good way of extracting Annotated from quoted annotations, so the special casing would probably be inconsistent between quoted and unquoted annotations, if this causes problems it may provide a compelling reason to actually start parsing the annotation expressions, instead of doing a simplified name extraction.

Daverball commented 3 weeks ago

This was partially implemented in #193 but we probably want to add soft-runtime uses for the names in the value expression and add a couple of additional test cases.