That is a Pylance error.
You can create a pyrightconfig.json file at the root of your workspace and define the files to be exclude-d from analysis or completely ignore-d:
{
"ignore": [
"**/*.ipynb",
],
}
You can even list up specific filenames:
{
"ignore": [
"notimportant.ipynb",
"test.ipynb",
],
}
Historical Notes:
It initially didn't work for Jupyter Notebooks (.ipynb):
https://github.com/microsoft/pylance-release/issues/2135
This happens because pyright doesn't see the file as a "*.ipynb". The file is being preprocessed (to combine all of the cells) in the notebook by the VS Code Python extension, and the resulting combined file is then passed to pyright for analysis.
The pylance team is actively working on changing the way this works. I'm going to transfer this bug to the pylance-release repo so it gets the attention it deserves.
That Github issue has since been resolved the fix was deployed as part of pylance 2022.8.51: https://github.com/microsoft/pylance-release/blob/main/CHANGELOG.md#2022851-31-august-2022-prerelease
Notable changes:
- ...
- Bug Fix: Ignoring *.ipynb files does not work (pylance-release#2135)
If it somehow still does not work, check the version of pylance on your VS Code.
Answer from Gino Mempin on Stack OverflowThat is a Pylance error.
You can create a pyrightconfig.json file at the root of your workspace and define the files to be exclude-d from analysis or completely ignore-d:
{
"ignore": [
"**/*.ipynb",
],
}
You can even list up specific filenames:
{
"ignore": [
"notimportant.ipynb",
"test.ipynb",
],
}
Historical Notes:
It initially didn't work for Jupyter Notebooks (.ipynb):
https://github.com/microsoft/pylance-release/issues/2135
This happens because pyright doesn't see the file as a "*.ipynb". The file is being preprocessed (to combine all of the cells) in the notebook by the VS Code Python extension, and the resulting combined file is then passed to pyright for analysis.
The pylance team is actively working on changing the way this works. I'm going to transfer this bug to the pylance-release repo so it gets the attention it deserves.
That Github issue has since been resolved the fix was deployed as part of pylance 2022.8.51: https://github.com/microsoft/pylance-release/blob/main/CHANGELOG.md#2022851-31-august-2022-prerelease
Notable changes:
- ...
- Bug Fix: Ignoring *.ipynb files does not work (pylance-release#2135)
If it somehow still does not work, check the version of pylance on your VS Code.
Gino's solution doesn't work for me as of VSCode 1.88.1 + Pylance v2024.4.1.
What worked for me was adding the following to my pyproject.toml:
[tool.pyright]
exclude = ["**/*ipynb"]
I coach a robotics team of middle school kids and it is important that all of the laptops are configured the same. When we clone our repo, VS Code will prompt them to enable type checking. I'd rather keep type checking off for now, so I really much prefer the warning to not come up at all. The kids are kind of quick to hit the default "Yes", which enables type checking. I have in my pyproject.toml
```
[tool.pyright] typeCheckingMode = "off"
```
And that is included in the repo. And even so, I still get the warning/suggestion
"Pylance has detected type annotations in your code and recommends enabling type checking. Would you like to change this setting?"
Sure, I can click "No" at that point, and it seems to keep pylance happy and it doesn't ask again, but I'd rather it not ask at all in the first place. Ideally I'd like to figure out a way to suppress the warning at the project level, so I can push the setting to everyone as part of the repo.
For example __init__.py imports
You can add the following settings in settings.json configuration file:
"python.analysis.diagnosticSeverityOverrides": { "reportUndefinedVariable": "none" }
Or you can search for python.analysis.diagnosticSeverityOverrides in the settings, click Add Item button to select "reportUndefinedVariable", "none":

Result:

Following the issue that @Jill Cheng linked in the comments, the pylance devs suggest using a # type: ignore comment after the line in question. You will lose out on other linting abilities for this line though so apply with caution.
As a side note, if you're using this to quiet the messages due to "import <module> could not be resolved" then you should look into correctly configuring your workspace rather than overriding the message. Here's an example answer to help you solve that issue.
There are many ways of forcing a type-checker to accept this.
Use
assert:from typing import Union def do_something(var: Union[T, None]): assert var is not None var.foo()
Raise some other exception:
from typing import Union def do_something(var: Union[T, None]): if var is None: raise RuntimeError("NO") var.foo()
Use an
ifstatement:from typing import Union def do_something(var: Union[T, None]): if var is not None: var.foo()
Use
typing.cast, a function that does nothing at runtime but forces a type-checker to accept that a variable is of a certain type:from typing import Union, cast def do_something(var: Union[T, None]): var = cast(T, var) var.foo()
Switch off the type-checker for that line:
from typing import Union def do_something(var: Union[T, None]): var.foo() # type: ignore
Note also that, while it makes no difference to how your type annotation is interpreted by a type-checker (the two are semantically identical), you can also write typing.Union[T, None] as typing.Optional[T], which is arguably slightly nicer syntax. In Python >=3.10 (or earlier if you have from __future__ import annotations at the top of your code), you can even write Union types with the | operator, i.e. T | None.
Please don't use blanket # type: ignore. Instead be specific on what linting error you want to ignore:
myOptionalVar.foo() # pyright: ignore[reportOptionalMemberAccess]
Above works in VSCode default linter Pylance which uses Pyright.
If you are using MyPy, you can use the less specific error-code:
myOptionalVar.foo() # type: ignore[attr-defined]
Pylance supports PEP 484
A number of existing or potential use cases for function annotations exist, which are incompatible with type hinting. These may confuse a static type checker. However, since type hinting annotations have no runtime behavior (other than evaluation of the annotation expression and storing annotations in the _annotations_ attribute of the function object), this does not make the program incorrect -- it just may cause a type checker to emit spurious warnings or errors.
To mark portions of the program that should not be covered by type hinting, you can use one or more of the following:
a # type: ignore comment;
a @no_type_check decorator on a class or function;
a custom class or function decorator marked with @no_type_check_decorator.
Alternatively you can create a pyrightconfig.json for Pyright (as that's what Pylance is using underneath) or a pyproject.toml in project's root directory, and specify which types of errors to ignore. You can see the error type in the hover widget where error messages appear.
pyrightconfig.json example:
{
"reportGeneralTypeIssues": false,
}
pyproject.toml example:
[tool.pyright]
reportGeneralTypeIssues = false
See Type Check Diagnostics Settings for more.
The top-voted answer is great but it may not solve the problem (it just ignores the type checker, right?). My answer does not solve the problem either, but I think it's helpful.
I suggest you 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)
(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))
{
"python.analysis.typeCheckingMode": "basic",
"python.analysis.diagnosticSeverityOverrides": {
"reportGeneralTypeIssues": "none"
}
}
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 think [str] is invalid/nonstandard 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?