Git Product home page Git Product logo

Comments (4)

erictraut avatar erictraut commented on May 28, 2024

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.

DiegoBaldassarMilleuno avatar DiegoBaldassarMilleuno commented on May 28, 2024

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.

erictraut avatar erictraut commented on May 28, 2024

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.

DiegoBaldassarMilleuno avatar DiegoBaldassarMilleuno commented on May 28, 2024

I see, thank you.

from mypy.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.