Comments (4)
I think mypy is correct here. Final
means that a value is both read-only and immutable. Your protocol definition here does not guarantee immutability of the value of _fields
, so Type2
, Type3
and Type4
are not compatible with the protocol.
Pyright also generates the same errors as mypy in this case.
A ReadOnly
special form was recently added to the type system as part of PEP 705. It currently works only within a TypedDict
definition, but there has been some discussion of supporting it in protocols and other classes. If this extension were made to the type system, it would allow you to do what you're trying to do here.
from mypy.
Thank you for the answer. So it seems that it's not currently possible.
However, I find it weird that mypy reports that
Only class variables allowed [...], _fields is an instance variable of "TypeX"
when it is not, it's a class variable. Isn't that a bug?
from mypy.
The protocol defines _fields
as a property, which means it should be interpreted as a read-only instance variable. That's how the typing spec indicates properties should be interpreted when they're used in protocols. Mypy is trying to tell you that if you access _fields
from the class object Type2
, you will get a tuple[str, str]
object, but if you try to access _fields
from a class object of type Proto2
, you will get a property
object.
So I understand why you would find the error message somewhat confusing, but it's consistent with the way protocols are defined to work.
from mypy.
I see, thank you.
from mypy.
Related Issues (20)
- Cannot find implementation or library stub for module HOT 1
- TypedDict totality ignored when instantiating a not type-hinted class variable typed dict
- Possible bug in mypy when attribute and parameter share name on same line. HOT 2
- Adding a field to a callback protocol changes the checks performed by mypy
- False positive [unreachable] with `TypeIs`
- Spurious "Invalid self argument ... to attribute function" error when currying TypeVarTuple
- mypy cannot determine Enum members from static or Final values HOT 3
- Return type `AsyncIterable` not correctly deduced for abstract methods HOT 2
- Weird error in 1.10.0 trying to parse site-packages\torch\_C\_VariableFunctions.pyi HOT 8
- Mypy.parse needs error messages HOT 5
- Regression since mypy 1.7 with functions that return a generic protocol HOT 2
- `safe-super` change breaks Mixin support
- Weird interaction between TypeVar and union
- Tuple of iterables are not narrowed after converting it to tuple of tuple HOT 6
- INTERNAL ERROR from types.py: assert isinstance(visitor, SyntheticTypeVisitor)
- Difference in behaviour between overload and TypeVars when using functools.update_wrapper
- False error: Never has no attribute "foo" [attr-defined]
- stubgen: combine `--include-docstrings` and typeshed
- Type invalid due to import, only on alternating runs HOT 1
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 mypy.