The top-voted answer is great but I can see why it's not accepted yet. My answer does not solve the problem either, but I think it's helpful.
Yet another alternative to the top-voted answer is to modify your .vscode/settings.json
(Workspace settings or User settings) as described here and here and shown below. (Notice the use of "none"
instead of false
)
{
"python.analysis.typeCheckingMode": "basic",
"python.analysis.diagnosticSeverityOverrides": {
"reportGeneralTypeIssues": "none"
}
}
(Maybe this is preferable if you already have Workspace settings and you don't want to add another settings file as shown in the top-voted answer (like pyrightconfig.json
or pyproject.toml
))
I think the OP is correct with this comment, this error is not a reportGeneralTypeIssues
type issue.
I've tried both solutions but the error still exists. Maybe it's not a reportGeneralTypeIssues?
If you reset your Pylance settings (flag all diagnostic codes as errors (remove settings.json, or use the "error" diagnostic setting)), and you type the OP's code below
def example() -> [str]:
return ["hi"]
VSCode/Pylance/Pyright shows an error squiggly, and when you hover it lists two error reasons.
- The first error says "List expression not allowed in type annotation Use List[T]..." and is tagged only as "
Pylance
" (no diagnostic code)
- The second error says "Expected type expression but received "list[Type[str]]" and is tagged as
Pylance(reportGeneralTypeIssues)
, (note there is a diagnostic code)

If you modify your settings.json as described above:
...
"python.analysis.diagnosticSeverityOverrides": {
"reportGeneralTypeIssues": "none"
}
...
... then the second Pylance(reportGeneralTypeIssues)
type error goes away, but the original Pylance
error (without the diagnostic code) is still there:

The top-voted answerer suggests trying other diagnostic codes, but it seems like this error is more a "core error" than any diagnostic code describes?
I think it's tempting to blame the editor (VSCode), but I don't think it's VSCode's fault, VSCode is just using Pylance, and Pylance is just trying to implement the Python standards/PEPs, right?
I still don't understand how [str]
is valid/standard type hinting syntax, as this commenter said .
I agree with OP, [str]
is "shorter and does the trick", but it's not standard is it? Not defined in any PEP? Not in PEP 484, not in PEP 585. Doesn't this mean PyCharm is supporting something nonstandard?