14

At the company I'm working we use type annotation to define the expected return type from a function. Most developers are using PyCharm but I prefer to stick with VS Code.

The following line in the VS Code IDE:

def example() -> [str]:

Raises a Pylance warning:

List expression not allowed in type annotation
  Use List[T] to indicate a list type or Union[T1, T2] to indicate a union typePylance

And would like me to use:

def example() -> List[str]

Although fixing this would require me to go over the whole code base and won't be accepted for a pull request. As I can live with this warning, I would like to suppress it.

Joep
  • 788
  • 2
  • 8
  • 23
  • 2
    why suffer with bad editor settings? tools should work for you, not you for the tool – ti7 Jun 11 '21 at 06:03
  • 4
    I haven't seen this style of type annotation used before. I know that from Python 3.9 this type annotation is now allowed `list[str]`, i.e. using parameterised generics, but I haven't seen `[str]` before. Is everyone on the team using the same type annotations? – NotAName Jun 11 '21 at 06:14
  • 1
    @pavel Yes, everyone on the team is using `[str]`, and I don't mind because it is shorter and does the trick. I guess that's why it's allowed in future versions. – Joep Jun 11 '21 at 06:58
  • Sounds like a bad practice that needs correction, rather than worked around via ignoring type checkers. Type annotations are ignored and do not raised runtime errors, so they really should consider using correct syntax - `list[str]`. I doubt they would annotate a function as `{str, Any}` afterall. – DataWizard Aug 21 '23 at 19:14

3 Answers3

11

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.

  • 1
    This solution requires to add code all over the code base, which I don't prefer, and won't be accepted for a pull request. – Joep Jun 11 '21 at 07:03
  • 1
    @Joep I've edited the answer, these are the only two options as far as I can tell. No pylance specific settings in VSCode's settings.json. – Alexandru Dreptu Jun 11 '21 at 08:44
  • 1
    Thanks for the edit, I've tried both solutions but the error still exists. Maybe it's not a reportGeneralTypeIssues? I think we're close – Joep Jun 11 '21 at 10:36
  • 1
    @Joep in my VSCode one of the errors for your example function was reportGeneralTypeIssues, so I used it as an example, the rest of possible error types are mentioned in the documentation I've linked. – Alexandru Dreptu Jun 11 '21 at 10:41
  • This worked for me, but I had to set the pyproject.toml line to `reportGeneralTypeIssues = "none"` rather than `false` – Scott Jun 06 '23 at 05:09
3

One additional option not always covered are inline ignores with specific rule names. This is great when working in a strict codebase that disallows the generic mypy style # type: ignore without a rule being specified.

Here is a common example, when type narrowing just isn't working:

# pyright: ignore[reportUnknownMemberType]

It's a little more involved than your typical # type: ignore[assignment], but works well for suppressing most Pylance errors. Note that it uses an array, so you can add more rules to ignore in-line as well.

# pyright: ignore[reportUnknownMemberType, reportGeneralTypeIssues]

Further, if you'd like to look up the specific rule names manually (such as when Pylance can't display them properly), give this list a try: pyright/.../diagnosticRules.ts

DataWizard
  • 61
  • 5
2

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)

enter image description here

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:

enter image description here

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?

Nate Anderson
  • 18,334
  • 18
  • 100
  • 135