how to suppress errors about a library with types set to 'UndefinedType'?
I suspect this is an issue not quite due solely to pylance or to the library in question (altair) but to the interaction of the two...
I am using pylance on a project that makes use of the altair data visualization library. Nearly every call to an altair method produces a pylance error about not assigning something to a parameter of type UndefinedType.
For example, altair's chart method takes a pandas.DataFrame object as an argument:
import altair
import pandas
df: pandas.DataFrame = pandas.DataFrame({'label': ['a', 'b', 'c'], 'count': [1, 2, 3]})
my_chart = altair.Chart(df)
However, the call to altair.Chart produces the following pylance error:
Argument of type "DataFrame" cannot be assigned to parameter "data" of type "UndefinedType" in function "__init__"
"DataFrame" is incompatible with "UndefinedType"
Pylance(reportGeneralTypeIssues)
For context, I believe what is happening is that the altair library has defined a class UndefinedType of which Undefined is an instance: https://github.com/altair-viz/altair/blob/137a6e52efd2cc38a9ba4fbba1c6a7e0a75801bc/altair/utils/schemapi.py#L126-L140
And then the alt.Chart method has an argument with Undefined as the default value, so the typer is inferring UndefinedType as the expected type of the argument.
Is there any way to suppress this class of errors arising from the altair library? For example, is there some way to tell pylance to treat UndefinedType (which is not exported as far as I can tell) as equivalent to Any ? (https://github.com/altair-viz/altair/pull/2670 is my attempt to do so directly, but it seems like it might be preferable to make this sort of change at the level of the typer instead.)
Environment data
- Language Server version: 2022.8.20
- OS and version: darwin x64
- Python version (and distribution if applicable, e.g. Anaconda):
- python.analysis.indexing: null
- python.analysis.typeCheckingMode: basic
Error details
[{
"resource": "/Users/brahn/pylance-demo.py",
"owner": "_generated_diagnostic_collection_name_#0",
"code": {
"value": "reportGeneralTypeIssues",
"target": {
"$mid": 1,
"external": "https://github.com/microsoft/pylance-release/blob/main/DIAGNOSTIC_SEVERITY_RULES.md#diagnostic-severity-rules",
"path": "/microsoft/pylance-release/blob/main/DIAGNOSTIC_SEVERITY_RULES.md",
"scheme": "https",
"authority": "github.com",
"fragment": "diagnostic-severity-rules"
}
},
"severity": 8,
"message": "Argument of type \"DataFrame\" cannot be assigned to parameter \"data\" of type \"UndefinedType\" in function \"__init__\"\n \"DataFrame\" is incompatible with \"UndefinedType\"",
"source": "Pylance",
"startLineNumber": 5,
"startColumn": 22,
"endLineNumber": 5,
"endColumn": 24
}]```
I suggest if you want the benefits of type checking to change chart(data) to be chart(data: Dataframe)
alternatively change Undefined to alias Any maybe?
Undefined = Any
but then again you are losing the benefits of using actual types.
see https://github.com/Microsoft/pylance-release#settings-and-customization on how to use diagnostic overrides, but I wouldn't suggest using that
{
"python.analysis.diagnosticSeverityOverrides": {
"reportGeneralTypeIssues": "none",
}
}
maybe the python typing group can help you https://github.com/python/typing/discussions
Thanks @bschnurr!
I suggest if you want the benefits of type checking to change chart(data) to be chart(data: Dataframe)
This doesn't change the behavior (which makes sense since the typechecker's objection is that data is not an instance of UndefinedType)
alternatively change Undefined to alias Any maybe?
In essence that's what I'm trying to accomplish with https://github.com/altair-viz/altair/pull/2670 (which does seem to solve my problem, if in an unpleasant fashion)
closing for now
Thanks @bschnurr !
Papertrail: https://github.com/altair-viz/altair/pull/2681 merged to solve my problem :-)