Comments (10)
no "Quick Fix" option to supress this as a type hint issue.
ya, I think we should fix this part. but for now, you can add one manually # type: ignore
until we fix it.
from pylance-release.
If SqlAlchemy is using this in a way that is disallowed by the current spec, they should either fix their usage or work with the typing community to come to an agreement about its intended meaning when ClassVar
is used in this manner. The typing spec can be amended to accommodate new use cases where they make sense. The Python typing forum is a good place to start a discussion.
from pylance-release.
Yeah, I will create an issue with SqlAlchemy and see what their opinion is first, perhaps my understanding of how SqlAlchemy is handeling this is wrong.
from pylance-release.
ClassVar
is a type qualifier that can be used only in specific contexts as specified in the Python typing spec. You're using it outside of an allowed context.
In short, ClassVar
has a valid meaning only when used to define a class-scoped variable. It's used to distinguish between an instance variable and a class variable within a class body. I'm not sure what you're trying to do here by using ClassVar
in a return type annotation, but it has no valid meaning here, and the typing spec specifically states that a type checker should generate an error in this case.
from pylance-release.
I beleive in this case that "baz_id" does indeed become a class variable.
Although I would need to dig deeper into the SqlAlchemy machanics to understand how.
from pylance-release.
I will create an issue on the SqlAlchemy side and see why they require ClassVar
in this case. But I still find the wording "not allowed in this context" confusing, as there is no runtime error and no "Quick Fix" option to supress this as a type hint issue.
from pylance-release.
I'm not sure how else to phrase the error. The typing spec is clear that ClassVar
can be used only in certain contexts. You're using it in a context that is invalid according to the spec. ClassVar
has no defined meaning in this context, so it's appropriate for a type checker to tell you "hey, this isn't doing what you think it's doing".
I don't think a quick fix makes sense here because this is a very unusual use case, and it's not clear what the "fix" would be other than to remove the ClassVar
from the return type annotation.
Suppressing this error wouldn't be a good idea because ClassVar
doesn't make any sense here, and it's not doing what you intended for it to do.
Please let us know if you discover what the original intent was. We may be able to help suggest an approach that works within the bounds of the typing spec.
from pylance-release.
I don't think a quick fix makes sense here because this is a very unusual use case, and it's not clear what the "fix" would be other than to remove the
ClassVar
from the return type annotation.
In many other cases the type hinter offers the option # type: ignore
, as sometimes the runtime requirements of a library do not match the typing spec,
I do agree, upon reading the spec, the wording is very difficult here. The confusion on my end though comes from the fact that Pylance will warn about things that aren't strictly type hint errors, so when I read "not allowed" I assumed this code would cause a runtime error.
from pylance-release.
Suppressing this error wouldn't be a good idea because
ClassVar
doesn't make any sense here, and it's not doing what you intended for it to do.
SqlAlchemy is using the annotations at runtime to define an ORM, I have confirmed the correct ORM gets produced with ClassVar
in SqlAlchemy 1.4 and 2.0, but if I ommit ClassVar
SqlAlchemy 2.0 produces a runtime error.
As the runtime behavior is more important to me than the type hint behavior, it is doing what I intend it to do.
from pylance-release.
FYI, SqlAlchemy authors reccomend not to use runtime type hints for this example: sqlalchemy/sqlalchemy#11377 (comment)
from pylance-release.
Related Issues (20)
- issue HOT 1
- Pylance doesn't honor VSCode display language HOT 4
- Docs should include guidance on using Pylance on low power devices HOT 5
- `Files to analyze` count doesn't stack in multi-workspace scenarios HOT 1
- Copying and pasting code from function body messes up with indentation HOT 4
- This popped up on vscode HOT 1
- Pylance does not report @abstractmethod not being implemented HOT 3
- It would be much better DX if missing abstract methods show up in class definition HOT 2
- a subclass member variable is not recognized as the same variable in superclass unless it has an explicit type hint HOT 1
- Pylance has collapsed. Want to try using the Node.js executable to run the language server? HOT 9
- Multiple libraries not recognized with errors is not a known attribute of module HOT 6
- Type inference causing problems when is_dataclass() is tested in decorator HOT 4
- `Import could not be resolved` with `[tool.setuptools.package-dir]` HOT 15
- Pylance reports import Missing in inexistent file when git enabled HOT 21
- Pylance still report problems in excluded or ignored directories, even in a closed file HOT 8
- Report HOT 1
- Pylance 2024.5.1 and 2024.4.1 crashes regularly, reverting back to 2024.3.2 runs stable HOT 8
- ReportUnnecessaryComparison False Positive HOT 1
- Warnings for unbound variables? HOT 3
- Cell magics disable parsing of python code HOT 8
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pylance-release.