Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to suppress errors about a library with types set to 'UndefinedType'? #3210

Closed
brahn opened this issue Aug 17, 2022 · 5 comments
Closed
Assignees
Labels
needs investigation Could be an issue - needs investigation

Comments

@brahn
Copy link

brahn commented Aug 17, 2022

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 ? (vega/altair#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
}]```
@bschnurr
Copy link
Member

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",
    }
}

@bschnurr
Copy link
Member

maybe the python typing group can help you
https://github.com/python/typing/discussions

@brahn
Copy link
Author

brahn commented Aug 18, 2022

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 vega/altair#2670 (which does seem to solve my problem, if in an unpleasant fashion)

@judej judej added the needs decision Do we want this enhancement? label Aug 22, 2022
@judej judej added needs investigation Could be an issue - needs investigation and removed needs decision Do we want this enhancement? labels Aug 23, 2022
@bschnurr
Copy link
Member

bschnurr commented Oct 13, 2022

closing for now

@brahn
Copy link
Author

brahn commented Oct 14, 2022

Thanks @bschnurr !

Papertrail: vega/altair#2681 merged to solve my problem :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs investigation Could be an issue - needs investigation
Projects
None yet
Development

No branches or pull requests

3 participants