{"id":20744791,"url":"https://github.com/shoobx/mypy-zope","last_synced_at":"2025-04-04T10:06:09.752Z","repository":{"id":38236841,"uuid":"164156652","full_name":"Shoobx/mypy-zope","owner":"Shoobx","description":"Plugin for mypy to support zope.interface","archived":false,"fork":false,"pushed_at":"2025-02-19T10:41:30.000Z","size":420,"stargazers_count":39,"open_issues_count":19,"forks_count":13,"subscribers_count":9,"default_branch":"master","last_synced_at":"2025-04-04T09:43:39.257Z","etag":null,"topics":["mypy","plugin","python","static-typing","zope"],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Shoobx.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-01-04T21:30:20.000Z","updated_at":"2025-02-19T08:14:31.000Z","dependencies_parsed_at":"2024-04-25T01:56:51.494Z","dependency_job_id":"610e3835-cd46-467c-93aa-8523f59875e9","html_url":"https://github.com/Shoobx/mypy-zope","commit_stats":{"total_commits":230,"total_committers":16,"mean_commits":14.375,"dds":"0.22608695652173916","last_synced_commit":"6945ee0ea814724712b75338c1b1967950a78f06"},"previous_names":[],"tags_count":43,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Shoobx%2Fmypy-zope","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Shoobx%2Fmypy-zope/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Shoobx%2Fmypy-zope/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Shoobx%2Fmypy-zope/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Shoobx","download_url":"https://codeload.github.com/Shoobx/mypy-zope/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247157281,"owners_count":20893220,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["mypy","plugin","python","static-typing","zope"],"created_at":"2024-11-17T07:17:13.808Z","updated_at":"2025-04-04T10:06:09.728Z","avatar_url":"https://github.com/Shoobx.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Plugin for mypy to support zope.interface\n\n[![Coverage Status](https://coveralls.io/repos/github/Shoobx/mypy-zope/badge.svg)](https://coveralls.io/github/Shoobx/mypy-zope)\n[![Checked with mypy](http://www.mypy-lang.org/static/mypy_badge.svg)](http://mypy-lang.org/)\n\nThe goal is to be able to make zope interfaces to be treated as types in mypy\nsense.\n\n## Usage\n\nInstall both mypy and mypy-zope:\n```sh\npip install mypy-zope\n```\n\nEdit `mypy.ini` file in your project to enable the plugin:\n\n```ini\n[mypy]\nnamespace_packages=True\nplugins=mypy_zope:plugin\n```\n\nYou're done! You can now check your project with mypy:\n\n```sh\nmypy your-project-dir\n```\n\n## What is supported?\n\nYou can browse\n[sample files](https://github.com/Shoobx/mypy-zope/tree/master/tests/samples)\nto get some sense on what features are supported and how they are handled.\n\n### Interface declarations\n\nYou can define the interface and provide implementation:\n\n```python\nclass IAnimal(zope.interface.Interface):\n    def say() -\u003e None:\n        pass\n\n@zope.interface.implementer(IAnimal)\nclass Cow(object):\n    def say(self) -\u003e None:\n        print(\"Moooo\")\n\nanimal: IAnimal = Cow()\nanimal.say()\n```\n\nThe interface `IAnimal` will be treated as superclass of the implementation\n`Cow`: you will be able to pass an implementation to functions accepting an\ninterface and all the usual polymorphism tricks.\n\nIt is also possible to declare the implementation using `classImplements`\nfunction  with the same effect as `@imlementer` decorator. This is useful if\nyou do not control the code that defines the implementation class.\n\n```python\nclassImplements(Cow, IAnimal)\n\nanimal: IAnimal = Cow()\n```\n\n### Schema field type inference\nA limited support for defining attributes as `zope.schema.Field`s is supported too:\n\n```python\nclass IAnimal(zope.interface.Interface):\n    number_of_legs = zope.schema.Int(title=\"Number of legs\")\n\n@zope.interface.implementer(IAnimal)\nclass Cow(object):\n    number_of_legs = 4\n```\n\nIn context of an interface, some known `zope.schema` field types are\nautomatically translated to python types, so the `number_of_legs` attributes is\ngetting the type `int` in the example above. That means mypy will report an\nerror if you try to assign string to that attribute on an instance of `IAnimal`\ntype. Custom fields or fields not recognized by plugin are given type `Any`.\n\n### Field properties\n\nSupport for `zope.schema.FieldProperty` is limited, because type information is\nnot transferred from an interface to implementation attribute, but mypy doesn't\nreport errors on sources like this:\n\n```python\nclass IAnimal(zope.interface.Interface):\n    number_of_legs = zope.schema.Int(title=\"Number of legs\")\n\n@zope.interface.implementer(IAnimal)\nclass Cow(object):\n    number_of_legs = zope.schema.FieldProperty(IAnimal['number_of_legs'])\n```\n\nThe type of `Cow.number_of_legs` will become `Any` in this case, even though\n`IAnimal.number_of_legs` would be inferred as `int`.\n\n### Adaptation pattern\n\nZope interfaces can be \"called\" to lookup an adapter, like this:\n\n```python\nclass IEUPowerSocket(zope.interface.Interface):\n    def fit():\n        pass\n\nadapter = IEUPowerSocket(us_plug)\nadapter.fit()\n```\n\nType of the `adapter` variable will be set to `IEUPowerSocket`.\n\n### Conditional type inference\n\nWhen using `zope.interface`'s `implementedBy()` and `providedBy()` methods\nin an if statement, `mypy` will know which type it is inside those statements.\n\n```python\nif IAnimal.providedBy(ob):\n    ob.number_of_legs += 2\n\n```\n\n### Declaration of overloaded methods in interfaces\n\nSimilarly to regular [overloaded\nfunctions](https://docs.python.org/3/library/typing.html#typing.overload),\n`@overload` declarations are supported in interfaces as well:\n\n```python\nclass IAnimal(zope.interface.Interface):\n    @overload\n    def say() -\u003e str:\n        ...\n\n    @overload\n    def say(count: int) -\u003e List[str]:\n        ...\n\n    def say(count: int = None) -\u003e Union[str, List[str]]:\n        pass\n\n\n@zope.interface.implementer(IAnimal)\nclass Cow(object):\n    @overload\n    def say(self) -\u003e str:\n        ...\n\n    @overload\n    def say(self, count: int) -\u003e List[str]:\n        ...\n\n    def say(self, count: int = None) -\u003e Union[str, List[str]]:\n        if count is None:\n            return \"Mooo\"\n        return [\"Mooo\"] * count\n```\n\n### Type stubs for zope.interface and zope.schema\n\n`mypy-zope` ships with type stubs (`*.pyi` files) for `zope.interface` and\n`zope.schema` packages. They are enabled automatically as soon as plugin is\nenabled.\n\n\n## What is not supported?\n\nThese `zope.interface` features are not supported:\n\n* Declaring modules as interface implementers.\n* Type inference for `zope.schema.List` and `zope.schema.Dict` fields.\n* Stub files are largely incomplete\n* Interface compatibility checker will not type-check non-method attributes\n\n## Ready to use!\n\nCurrently, the project is used in production in various substantially large\nprojects and considered production-grade, however there still might be subtle\nbugs around. Suggestions and pull requests are welcomed!\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshoobx%2Fmypy-zope","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fshoobx%2Fmypy-zope","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshoobx%2Fmypy-zope/lists"}