fix: only raise TC101 for str literals#122
Merged
Conversation
Codecov Report
@@ Coverage Diff @@
## main #122 +/- ##
=====================================
Coverage 98.7% 98.7%
=====================================
Files 3 3
Lines 466 466
=====================================
Hits 460 460
Misses 6 6
|
sondrelg
approved these changes
Aug 4, 2022
Member
sondrelg
left a comment
There was a problem hiding this comment.
I think your example for where it doesn't might be wrong, but PR looks good! I can't think of a reason why we wouldn't limit this to string literals.
And wrt. hardcoding generic types. It seems like it could make sense to add a set of hardcoded common types at some point, for this purpose.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR changes the code related to the TC101 check to only show errors if there's a
strliteral/constant, not any other type.There are valid use-cases for literals that don't represent types in annotation subscripts, especially with the introduction of
typing.Annotated, likex: Annotated[int, 42]or generally custom types with__class_getitem__.Adding on to this, I suppose
Annotated[int, "stuff"]should technically also be allowed, but it's not possible to generally differentiate between contexts where a string literal is meant to represent a type (e.g.Dict[str, "int"]), and where it doesn't (e.g.Annotated[str, "int"]), without hardcoding specific generic types.As such, this is by far not a perfect solution, but it should get a bit closer :)
For context, this first appeared here:
https://github.com/DisnakeDev/disnake/blob/dd1fb9e4cbfd5974bf4e2ddf001aecc7b6443478/examples/slash_commands/param.py#L76-L78