René's URL Explorer Experiment


Title: Read-only `Protocol` attributes · python/typing · Discussion #903 · GitHub

Open Graph Title: Read-only `Protocol` attributes · python/typing · Discussion #903

X Title: Read-only `Protocol` attributes · python/typing · Discussion #903

Description: Read-only `Protocol` attributes

Open Graph Description: Is there a way to mark an attribute of a Protocol as read-only so that objects with read-write attributes as well as read-only properties can be assigned to that protocol. In TypeScript, I would do...

X Description: Is there a way to mark an attribute of a Protocol as read-only so that objects with read-write attributes as well as read-only properties can be assigned to that protocol. In TypeScript, I would do...

Opengraph URL: https://github.com/python/typing/discussions/903

X: @github

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"QAPage","mainEntity":{"@type":"Question","name":"Read-only `Protocol` attributes","text":"\u003cp dir=\"auto\"\u003eIs there a way to mark an attribute of a \u003ccode class=\"notranslate\"\u003eProtocol\u003c/code\u003e as read-only so that objects with read-write attributes as well as read-only properties can be assigned to that protocol. In TypeScript, I would do something like this:\u003c/p\u003e\n\u003cdiv class=\"highlight highlight-source-ts notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"interface Identifiable {\n  readonly id: number;\n}\n\nclass Person {\n  id: number;\n  name: string;\n\n  constructor(id: number, name: string) {\n    this.id = id;\n    this.name = name;\n  }\n}\n\nclass Animal {\n  readonly id: number;\n  species: string;\n\n  constructor(id: number, species: string) {\n    this.id = id;\n    this.species = species;\n  }\n}\n\nfunction printID(object: Identifiable) {\n  console.log(object.id);\n  // object.id = 2 would error because of the readonly property\n}\n\nconst person = new Person(1, 'John Doe');\nconst animal = new Animal(2, 'Dog');\n\nprintID(person); // OK\nprintID(animal); // OK\"\u003e\u003cpre class=\"notranslate\"\u003e\u003cspan class=\"pl-k\"\u003einterface\u003c/span\u003e \u003cspan class=\"pl-smi\"\u003eIdentifiable\u003c/span\u003e \u003cspan class=\"pl-kos\"\u003e{\u003c/span\u003e\n  \u003cspan class=\"pl-k\"\u003ereadonly\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003eid\u003c/span\u003e: \u003cspan class=\"pl-smi\"\u003enumber\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e;\u003c/span\u003e\n\u003cspan class=\"pl-kos\"\u003e}\u003c/span\u003e\n\n\u003cspan class=\"pl-k\"\u003eclass\u003c/span\u003e \u003cspan class=\"pl-smi\"\u003ePerson\u003c/span\u003e \u003cspan class=\"pl-kos\"\u003e{\u003c/span\u003e\n  \u003cspan class=\"pl-c1\"\u003eid\u003c/span\u003e: \u003cspan class=\"pl-smi\"\u003enumber\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e;\u003c/span\u003e\n  \u003cspan class=\"pl-c1\"\u003ename\u003c/span\u003e: \u003cspan class=\"pl-smi\"\u003estring\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e;\u003c/span\u003e\n\n  \u003cspan class=\"pl-en\"\u003econstructor\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e(\u003c/span\u003e\u003cspan class=\"pl-s1\"\u003eid\u003c/span\u003e: \u003cspan class=\"pl-smi\"\u003enumber\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e,\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003ename\u003c/span\u003e: \u003cspan class=\"pl-smi\"\u003estring\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e)\u003c/span\u003e \u003cspan class=\"pl-kos\"\u003e{\u003c/span\u003e\n    \u003cspan class=\"pl-smi\"\u003ethis\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e.\u003c/span\u003e\u003cspan class=\"pl-c1\"\u003eid\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003e=\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003eid\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e;\u003c/span\u003e\n    \u003cspan class=\"pl-smi\"\u003ethis\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e.\u003c/span\u003e\u003cspan class=\"pl-c1\"\u003ename\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003e=\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003ename\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e;\u003c/span\u003e\n  \u003cspan class=\"pl-kos\"\u003e}\u003c/span\u003e\n\u003cspan class=\"pl-kos\"\u003e}\u003c/span\u003e\n\n\u003cspan class=\"pl-k\"\u003eclass\u003c/span\u003e \u003cspan class=\"pl-smi\"\u003eAnimal\u003c/span\u003e \u003cspan class=\"pl-kos\"\u003e{\u003c/span\u003e\n  \u003cspan class=\"pl-k\"\u003ereadonly\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003eid\u003c/span\u003e: \u003cspan class=\"pl-smi\"\u003enumber\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e;\u003c/span\u003e\n  \u003cspan class=\"pl-c1\"\u003especies\u003c/span\u003e: \u003cspan class=\"pl-smi\"\u003estring\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e;\u003c/span\u003e\n\n  \u003cspan class=\"pl-en\"\u003econstructor\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e(\u003c/span\u003e\u003cspan class=\"pl-s1\"\u003eid\u003c/span\u003e: \u003cspan class=\"pl-smi\"\u003enumber\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e,\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003especies\u003c/span\u003e: \u003cspan class=\"pl-smi\"\u003estring\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e)\u003c/span\u003e \u003cspan class=\"pl-kos\"\u003e{\u003c/span\u003e\n    \u003cspan class=\"pl-smi\"\u003ethis\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e.\u003c/span\u003e\u003cspan class=\"pl-c1\"\u003eid\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003e=\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003eid\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e;\u003c/span\u003e\n    \u003cspan class=\"pl-smi\"\u003ethis\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e.\u003c/span\u003e\u003cspan class=\"pl-c1\"\u003especies\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003e=\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003especies\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e;\u003c/span\u003e\n  \u003cspan class=\"pl-kos\"\u003e}\u003c/span\u003e\n\u003cspan class=\"pl-kos\"\u003e}\u003c/span\u003e\n\n\u003cspan class=\"pl-k\"\u003efunction\u003c/span\u003e \u003cspan class=\"pl-en\"\u003eprintID\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e(\u003c/span\u003e\u003cspan class=\"pl-s1\"\u003eobject\u003c/span\u003e: \u003cspan class=\"pl-smi\"\u003eIdentifiable\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e)\u003c/span\u003e \u003cspan class=\"pl-kos\"\u003e{\u003c/span\u003e\n  \u003cspan class=\"pl-smi\"\u003econsole\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e.\u003c/span\u003e\u003cspan class=\"pl-en\"\u003elog\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e(\u003c/span\u003e\u003cspan class=\"pl-s1\"\u003eobject\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e.\u003c/span\u003e\u003cspan class=\"pl-c1\"\u003eid\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e)\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e;\u003c/span\u003e\n  \u003cspan class=\"pl-c\"\u003e// object.id = 2 would error because of the readonly property\u003c/span\u003e\n\u003cspan class=\"pl-kos\"\u003e}\u003c/span\u003e\n\n\u003cspan class=\"pl-k\"\u003econst\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003eperson\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003e=\u003c/span\u003e \u003cspan class=\"pl-k\"\u003enew\u003c/span\u003e \u003cspan class=\"pl-v\"\u003ePerson\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e(\u003c/span\u003e\u003cspan class=\"pl-c1\"\u003e1\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e,\u003c/span\u003e \u003cspan class=\"pl-s\"\u003e'John Doe'\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e)\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e;\u003c/span\u003e\n\u003cspan class=\"pl-k\"\u003econst\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003eanimal\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003e=\u003c/span\u003e \u003cspan class=\"pl-k\"\u003enew\u003c/span\u003e \u003cspan class=\"pl-v\"\u003eAnimal\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e(\u003c/span\u003e\u003cspan class=\"pl-c1\"\u003e2\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e,\u003c/span\u003e \u003cspan class=\"pl-s\"\u003e'Dog'\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e)\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e;\u003c/span\u003e\n\n\u003cspan class=\"pl-en\"\u003eprintID\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e(\u003c/span\u003e\u003cspan class=\"pl-s1\"\u003eperson\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e)\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e;\u003c/span\u003e \u003cspan class=\"pl-c\"\u003e// OK\u003c/span\u003e\n\u003cspan class=\"pl-en\"\u003eprintID\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e(\u003c/span\u003e\u003cspan class=\"pl-s1\"\u003eanimal\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e)\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e;\u003c/span\u003e \u003cspan class=\"pl-c\"\u003e// OK\u003c/span\u003e\u003c/pre\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eUsing \u003ccode class=\"notranslate\"\u003eProtocol\u003c/code\u003e, I can only seemingly get so far:\u003c/p\u003e\n\u003cdiv class=\"highlight highlight-source-python notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"from typing import Protocol\n\nclass Identifiable(Protocol):\n    id: int    # how to mark this as read-only?\n\nclass Person:\n    id: int\n    name: str\n\n    def __init__(self, id: int, name: str) -\u0026gt; None:\n        self.id = id\n        self.name = name\n\nclass Animal:\n    __id: int\n    species: str\n\n    def __init__(self, id: int, species: str) -\u0026gt; None:\n        self.__id = id\n        self.species = species\n\n    @property\n    def id(self) -\u0026gt; int:\n        return self.__id\n\ndef print_id(object: Identifiable) -\u0026gt; None:\n    print(object.id)\n    # object.id = 2 will not error because id is declared read/write\n\nperson = Person(1, 'John Doe')\nanimal = Animal(2, 'Dog')\n\nprint_id(person)  # OK\nprint_id(animal)  # not OK because animal.id is not writable\"\u003e\u003cpre class=\"notranslate\"\u003e\u003cspan class=\"pl-k\"\u003efrom\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003etyping\u003c/span\u003e \u003cspan class=\"pl-k\"\u003eimport\u003c/span\u003e \u003cspan class=\"pl-v\"\u003eProtocol\u003c/span\u003e\n\n\u003cspan class=\"pl-k\"\u003eclass\u003c/span\u003e \u003cspan class=\"pl-v\"\u003eIdentifiable\u003c/span\u003e(\u003cspan class=\"pl-v\"\u003eProtocol\u003c/span\u003e):\n    \u003cspan class=\"pl-s1\"\u003eid\u003c/span\u003e: \u003cspan class=\"pl-smi\"\u003eint\u003c/span\u003e    \u003cspan class=\"pl-c\"\u003e# how to mark this as read-only?\u003c/span\u003e\n\n\u003cspan class=\"pl-k\"\u003eclass\u003c/span\u003e \u003cspan class=\"pl-v\"\u003ePerson\u003c/span\u003e:\n    \u003cspan class=\"pl-s1\"\u003eid\u003c/span\u003e: \u003cspan class=\"pl-smi\"\u003eint\u003c/span\u003e\n    \u003cspan class=\"pl-s1\"\u003ename\u003c/span\u003e: \u003cspan class=\"pl-smi\"\u003estr\u003c/span\u003e\n\n    \u003cspan class=\"pl-k\"\u003edef\u003c/span\u003e \u003cspan class=\"pl-en\"\u003e__init__\u003c/span\u003e(\u003cspan class=\"pl-s1\"\u003eself\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003eid\u003c/span\u003e: \u003cspan class=\"pl-smi\"\u003eint\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003ename\u003c/span\u003e: \u003cspan class=\"pl-smi\"\u003estr\u003c/span\u003e) \u003cspan class=\"pl-c1\"\u003e-\u0026gt;\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003eNone\u003c/span\u003e:\n        \u003cspan class=\"pl-s1\"\u003eself\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003eid\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003e=\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003eid\u003c/span\u003e\n        \u003cspan class=\"pl-s1\"\u003eself\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003ename\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003e=\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003ename\u003c/span\u003e\n\n\u003cspan class=\"pl-k\"\u003eclass\u003c/span\u003e \u003cspan class=\"pl-v\"\u003eAnimal\u003c/span\u003e:\n    \u003cspan class=\"pl-s1\"\u003e__id\u003c/span\u003e: \u003cspan class=\"pl-smi\"\u003eint\u003c/span\u003e\n    \u003cspan class=\"pl-s1\"\u003especies\u003c/span\u003e: \u003cspan class=\"pl-smi\"\u003estr\u003c/span\u003e\n\n    \u003cspan class=\"pl-k\"\u003edef\u003c/span\u003e \u003cspan class=\"pl-en\"\u003e__init__\u003c/span\u003e(\u003cspan class=\"pl-s1\"\u003eself\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003eid\u003c/span\u003e: \u003cspan class=\"pl-smi\"\u003eint\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003especies\u003c/span\u003e: \u003cspan class=\"pl-smi\"\u003estr\u003c/span\u003e) \u003cspan class=\"pl-c1\"\u003e-\u0026gt;\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003eNone\u003c/span\u003e:\n        \u003cspan class=\"pl-s1\"\u003eself\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003e__id\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003e=\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003eid\u003c/span\u003e\n        \u003cspan class=\"pl-s1\"\u003eself\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003especies\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003e=\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003especies\u003c/span\u003e\n\n    \u003cspan class=\"pl-en\"\u003e@\u003cspan class=\"pl-en\"\u003eproperty\u003c/span\u003e\u003c/span\u003e\n    \u003cspan class=\"pl-k\"\u003edef\u003c/span\u003e \u003cspan class=\"pl-en\"\u003eid\u003c/span\u003e(\u003cspan class=\"pl-s1\"\u003eself\u003c/span\u003e) \u003cspan class=\"pl-c1\"\u003e-\u0026gt;\u003c/span\u003e \u003cspan class=\"pl-smi\"\u003eint\u003c/span\u003e:\n        \u003cspan class=\"pl-k\"\u003ereturn\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003eself\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003e__id\u003c/span\u003e\n\n\u003cspan class=\"pl-k\"\u003edef\u003c/span\u003e \u003cspan class=\"pl-en\"\u003eprint_id\u003c/span\u003e(\u003cspan class=\"pl-s1\"\u003eobject\u003c/span\u003e: \u003cspan class=\"pl-smi\"\u003eIdentifiable\u003c/span\u003e) \u003cspan class=\"pl-c1\"\u003e-\u0026gt;\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003eNone\u003c/span\u003e:\n    \u003cspan class=\"pl-en\"\u003eprint\u003c/span\u003e(\u003cspan class=\"pl-s1\"\u003eobject\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003eid\u003c/span\u003e)\n    \u003cspan class=\"pl-c\"\u003e# object.id = 2 will not error because id is declared read/write\u003c/span\u003e\n\n\u003cspan class=\"pl-s1\"\u003eperson\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003e=\u003c/span\u003e \u003cspan class=\"pl-en\"\u003ePerson\u003c/span\u003e(\u003cspan class=\"pl-c1\"\u003e1\u003c/span\u003e, \u003cspan class=\"pl-s\"\u003e'John Doe'\u003c/span\u003e)\n\u003cspan class=\"pl-s1\"\u003eanimal\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003e=\u003c/span\u003e \u003cspan class=\"pl-en\"\u003eAnimal\u003c/span\u003e(\u003cspan class=\"pl-c1\"\u003e2\u003c/span\u003e, \u003cspan class=\"pl-s\"\u003e'Dog'\u003c/span\u003e)\n\n\u003cspan class=\"pl-en\"\u003eprint_id\u003c/span\u003e(\u003cspan class=\"pl-s1\"\u003eperson\u003c/span\u003e)  \u003cspan class=\"pl-c\"\u003e# OK\u003c/span\u003e\n\u003cspan class=\"pl-en\"\u003eprint_id\u003c/span\u003e(\u003cspan class=\"pl-s1\"\u003eanimal\u003c/span\u003e)  \u003cspan class=\"pl-c\"\u003e# not OK because animal.id is not writable\u003c/span\u003e\u003c/pre\u003e\u003c/div\u003e","upvoteCount":2,"answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"\u003cp dir=\"auto\"\u003eYou can use a \u003ccode class=\"notranslate\"\u003eProtocol\u003c/code\u003e with a property that does not have a setter:\u003c/p\u003e\n\u003cdiv class=\"highlight highlight-source-python notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"class Identifiable(Protocol):\n    @property\n    def id(self) -\u0026gt; int: ...\"\u003e\u003cpre class=\"notranslate\"\u003e\u003cspan class=\"pl-k\"\u003eclass\u003c/span\u003e \u003cspan class=\"pl-v\"\u003eIdentifiable\u003c/span\u003e(\u003cspan class=\"pl-v\"\u003eProtocol\u003c/span\u003e):\n    \u003cspan class=\"pl-en\"\u003e@\u003cspan class=\"pl-en\"\u003eproperty\u003c/span\u003e\u003c/span\u003e\n    \u003cspan class=\"pl-k\"\u003edef\u003c/span\u003e \u003cspan class=\"pl-en\"\u003eid\u003c/span\u003e(\u003cspan class=\"pl-s1\"\u003eself\u003c/span\u003e) \u003cspan class=\"pl-c1\"\u003e-\u0026gt;\u003c/span\u003e \u003cspan class=\"pl-smi\"\u003eint\u003c/span\u003e: ...\u003c/pre\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eNow \u003ccode class=\"notranslate\"\u003eobject.id = 2\u003c/code\u003e fails with a readable mypy error: \u003ccode class=\"notranslate\"\u003easd.py:29: error: Property \"id\" defined in \"Identifiable\" is read-only\u003c/code\u003e\u003c/p\u003e\n\u003cp dir=\"auto\"\u003eAs a side note, it's might not be a good idea to make a variable named \u003ccode class=\"notranslate\"\u003eid\u003c/code\u003e or \u003ccode class=\"notranslate\"\u003eobject\u003c/code\u003e, as those are names of built-ins, and it's not clear whether \u003ccode class=\"notranslate\"\u003eobject\u003c/code\u003e somewhere in the middle of the code refers to \u003ccode class=\"notranslate\"\u003eobject\u003c/code\u003e the built-in class or some variable named \u003ccode class=\"notranslate\"\u003eobject\u003c/code\u003e. This is worst with global variables, but I don't consider it good practice with local variables either.\u003c/p\u003e","upvoteCount":2,"url":"https://github.com/python/typing/discussions/903#discussioncomment-1430899"}}}

react-profiling0
react-import-mapreact,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322
route-pattern/_view_fragments/Voltron::DiscussionsFragmentsController/show/:user_id/:repository/:discussion_number/discussion_layout(.:format)
route-controllervoltron_discussions_fragments
route-actiondiscussion_layout
fetch-noncev2:a58a7f99-7569-4a91-8b71-d1de4fcbe1b5
current-catalog-service-hash9f0abe34da433c9b6db74bffa2466494a717b579a96b30a5d252e5090baea7be
request-id81BE:AE7DD:26A185:36286B:6B006A6A
html-safe-nonce61dafe99cc31cf7550d1be9435b8a98778ef7792f59f8c8f2dea25a935ff9789
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4MUJFOkFFN0REOjI2QTE4NTozNjI4NkI6NkIwMDZBNkEiLCJ2aXNpdG9yX2lkIjoiNzI0NTM2NjA2MDY4ODM3MDI4MiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9
visitor-hmac9604ae9dae3636852f3cf9a3178b8b28276000ece8580820670dc3041f509a4b
hovercard-subject-tagdiscussion:3608947
github-keyboard-shortcutsrepository,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/discussions_fragments/discussion_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/Voltron::DiscussionsFragmentsController/show/python/typing/903/discussion_layout
twitter:imagehttps://opengraph.githubassets.com/78885e7d57726ccbfece1ad62aa731fda8d10391c78e2f156af57ee1a25592d1/python/typing/discussions/903
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/78885e7d57726ccbfece1ad62aa731fda8d10391c78e2f156af57ee1a25592d1/python/typing/discussions/903
og:image:altIs there a way to mark an attribute of a Protocol as read-only so that objects with read-write attributes as well as read-only properties can be assigned to that protocol. In TypeScript, I would do...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
hostnamegithub.com
expected-hostnamegithub.com
None6527e91c60c0b29d16606ff32ac5df8ecdc37a2a62205ea057a14d7762c870fa
turbo-cache-controlno-preview
go-importgithub.com/python/typing git https://github.com/python/typing.git
octolytics-dimension-user_id1525981
octolytics-dimension-user_loginpython
octolytics-dimension-repository_id24604008
octolytics-dimension-repository_nwopython/typing
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id24604008
octolytics-dimension-repository_network_root_nwopython/typing
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release46f1cd2ae3cd8ae1a81019b5d619742e76f13bbd
ui-targetfull
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://github.com/python/typing/discussions/903#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Ftyping%2Fdiscussions%2F903
GitHubRepository hosting, code review, and collaborationhttps://github.com/platform
CopilotAI coding agent for tasks, projects, and automated workhttps://github.com/features/copilot
ActionsCI/CD and automationhttps://github.com/features/actions
Code QualityQuality analysis, fixes, and enforcementhttps://github.com/features/code-quality
CodespacesCloud development environmentshttps://github.com/features/codespaces
Code SecurityVulnerability detection and remediationhttps://github.com/security/advanced-security/code-security
Secret ProtectionSecret scanning and leak preventionhttps://github.com/security/advanced-security/secret-protection
All Featureshttps://github.com/features
Documentationhttps://docs.github.com/en
Traininghttps://learn.github.com/skills
Certificationshttps://learn.github.com/credentials
Build with Copilothttps://github.com/features/ai
Customize Copilothttps://docs.github.com/en/copilot/how-tos/copilot-on-github/customize-copilot/customize-copilot-overview
Integrationshttps://github.com/marketplace
Trending reposhttps://github.com/trending
GitHub Sponsorshttps://github.com/open-source/sponsors
Developer articleshttps://github.com/resources/articles
ChangelogYour source for the latest features, improvements, and fixes across GitHub products, all in one place.https://github.blog/changelog/
Enterpriseshttps://github.com/enterprise
Teamhttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
See all industrieshttps://github.com/solutions/industry
Why GitHubhttps://github.com/why-github
Customer Storieshttps://github.com/customer-stories
Business insightshttps://github.com/solutions/executive-insights
Trust Centerhttps://github.com/trust-center
GitHub Advanced SecurityEnterprise-grade code securityhttps://github.com/security/advanced-security
Copilot for businessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/enterprise/premium-support
Documentationhttps://docs.github.com/en
Resource centerhttps://github.com/resources
Bloghttps://github.blog/
Events & webinarshttps://github.com/resources/events
Community Forumhttps://github.com/orgs/community/discussions
Customer Storieshttps://github.com/customer-stories
Integrationshttps://github.com/marketplace
Customer supporthttps://help.github.com/
Traininghttps://learn.github.com/skills
GitHub Statushttps://www.githubstatus.com/
Abouthttps://github.com/about
Partnershttps://github.com/partners
Changeloghttps://github.blog/changelog/
GitHub UniverseThe event for developers, by developers — talks, workshops, and what’s next for GitHub.https://githubuniverse.com/
Pricinghttps://github.com/pricing
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Ftyping%2Fdiscussions%2F903
Sign uphttps://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fdiscussions_fragments%2Fdiscussion_layout&source=header-repo&source_repo=python%2Ftyping
Reloadhttps://github.com/python/typing/discussions/903
Reloadhttps://github.com/python/typing/discussions/903
Reloadhttps://github.com/python/typing/discussions/903
pythonhttps://github.com/python
typinghttps://github.com/python/typing
Codehttps://github.com/python/typing
Issues206 (206)https://github.com/python/typing/issues
Pull requests27 (27)https://github.com/python/typing/pulls
Discussionshttps://github.com/python/typing/discussions
Actionshttps://github.com/python/typing/actions
Security and qualityhttps://github.com/python/typing/security
Insightshttps://github.com/python/typing/insights
Answered https://github.com/python/typing/discussions/903#discussioncomment-1430899
Akulihttps://github.com/Akuli
bryanforbes https://github.com/bryanforbes
Q&Ahttps://github.com/python/typing/discussions/categories/q-a
Read-only `Protocol` attributes https://github.com/python/typing/discussions/903#top
bryanforbes https://github.com/bryanforbes
Answered https://github.com/python/typing/discussions/903#discussioncomment-1430899
Akulihttps://github.com/Akuli
Return to tophttps://github.com/python/typing/discussions/903#top
Please reload this pagehttps://github.com/python/typing/discussions/903
https://github.com/python/typing/discussions/903
bryanforbes https://github.com/bryanforbes
Oct 5, 2021 https://github.com/python/typing/discussions/903#discussion-3608947
Akuli https://github.com/Akuli
Oct 5, 2021 https://github.com/python/typing/discussions/903#discussioncomment-1430899
View full answer https://github.com/python/typing/discussions/903#discussioncomment-1430899
Oldest https://github.com/python/typing/discussions/903?sort=old
Newest https://github.com/python/typing/discussions/903?sort=new
Top https://github.com/python/typing/discussions/903?sort=top
Please reload this pagehttps://github.com/python/typing/discussions/903
https://github.com/python/typing/discussions/903
Akuli https://github.com/Akuli
Oct 5, 2021 https://github.com/python/typing/discussions/903#discussioncomment-1430899
https://github.com/jonemo
Please reload this pagehttps://github.com/python/typing/discussions/903
https://github.com/python/typing/discussions/903
jonemohttps://github.com/jonemo
Feb 4, 2023 https://github.com/python/typing/discussions/903#discussioncomment-4867020
Explicitly declaring implementationhttps://peps.python.org/pep-0544/#explicitly-declaring-implementation
explicit is better than implicithttps://peps.python.org/pep-0020/
https://github.com/Kentzo
Please reload this pagehttps://github.com/python/typing/discussions/903
https://github.com/python/typing/discussions/903
Kentzohttps://github.com/Kentzo
Feb 4, 2023 https://github.com/python/typing/discussions/903#discussioncomment-4867245
https://github.com/hauntsaninja
Please reload this pagehttps://github.com/python/typing/discussions/903
https://github.com/python/typing/discussions/903
Please reload this pagehttps://github.com/python/typing/discussions/903
https://github.com/python/typing/discussions/903
hauntsaninjahttps://github.com/hauntsaninja
Feb 4, 2023 https://github.com/python/typing/discussions/903#discussioncomment-4867254
https://github.com/jonemo
Please reload this pagehttps://github.com/python/typing/discussions/903
https://github.com/python/typing/discussions/903
jonemohttps://github.com/jonemo
Feb 4, 2023 https://github.com/python/typing/discussions/903#discussioncomment-4867300
https://github.com/sigma67
Please reload this pagehttps://github.com/python/typing/discussions/903
https://github.com/python/typing/discussions/903
sigma67https://github.com/sigma67
Aug 17, 2023 https://github.com/python/typing/discussions/903#discussioncomment-6751488
https://mypy.readthedocs.io/en/stable/protocols.htmlhttps://mypy.readthedocs.io/en/stable/protocols.html
bryanforbeshttps://github.com/bryanforbes
Sign up for freehttps://github.com/join?source=comment-repo
Sign in to commenthttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Ftyping%2Fdiscussions%2F903
🙏 Q&A https://github.com/python/typing/discussions/categories/q-a
https://github.com/bryanforbes
https://github.com/srittau
https://github.com/Kentzo
https://github.com/jonemo
https://github.com/hauntsaninja
https://github.com/sigma67
https://github.com/Akuli
https://github.com/python/typing/discussions/903
https://github.com/settings/replies?return_to=1
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.