jonesus / pony-stubs Goto Github PK
View Code? Open in Web Editor NEWPython type stubs for Pony ORM
Home Page: https://pypi.org/project/pony-stubs/
License: MIT License
Python type stubs for Pony ORM
Home Page: https://pypi.org/project/pony-stubs/
License: MIT License
Should the package be named pony-stubs
, types-pony
or something else?
@fluffy-critter suggested that
Also, just as a note, a more standard name for the types package would be types-pony.
But as far as I could find, this PEP (https://peps.python.org/pep-0561/#type-checker-module-resolution-order) suggests that type-only modules should be named <package_name>-stubs
. Personally I don't have an opinion other than that we go as standard as possible.
@fluffy-critter can you find another PEP that would propose the naming you suggested, or is it more based on what is commonly done?
Pony ORM uses quite a bunch of dynamic behaviour, which makes it hard to statically type. One of the most glaring difficulties I've had so far is with "attribute lifting" of related models: https://docs.ponyorm.org/working_with_relationships.html#attribute-lifting
Currently the workaround is to type all additional attributes of a SetInstance[EntityType]
as any (https://github.com/Jonesus/pony-stubs/blob/main/pony-stubs/orm/core.pyi#L464), but this is obviously suboptimal.
I have a model class like:
class User(orm.core.Entity):
name = orm.PrimaryKey(str)
profile = orm.Required(orm.json)
When I attempt to make use of this field, mypy fails to see orm.json
as being equivalent to a dict
, so I can neither call user.profile.copy()
nor can I return user.profile
as a dict directly.
When I have a model like:
class Foo(orm.core.Entity):
name = orm.PrimaryKey(str)
bar = orm.Set("Bar")
class Bar(orm.core.Entity):
foo = orm.Set(Foo)
mypy generates errors such as need type annotation for "name"
and need type annotation for "bar"
. I'm able to work around this by adding annotations such as:
class Foo(orm.core.Entity):
name:orm.PrimaryKey[str] = orm.PrimaryKey(str)
and so on.
It looks like one of the hairier type-inference things that Pony screws up mypy on is still happening even with this package installed. For example:
publ/path_alias.py:65: error: "Type[PathAlias]" has no attribute "__iter__" (not iterable)
publ/path_alias.py:74: error: "Type[PathAlias]" has no attribute "__iter__" (not iterable)
publ/path_alias.py:77: error: "Type[PathAlias]" has no attribute "__iter__" (not iterable)
publ/entry.py:746: error: "Type[Entry]" has no attribute "__iter__" (not iterable)
publ/entry.py:890: error: "Type[EntryAuth]" has no attribute "__iter__" (not iterable)
publ/entry.py:1008: error: "Type[PathAlias]" has no attribute "__iter__" (not iterable)
publ/entry.py:1011: error: "Type[Entry]" has no attribute "__iter__" (not iterable)
publ/category.py:72: error: "Type[Entry]" has no attribute "__iter__" (not iterable)
The respective lines of code are all simple generator-based queries, e.g.:
# path_alias.py:65
orm.delete(p for p in model.PathAlias if p.path == path)
# entry.py:746
limit = max(10, orm.get(orm.count(e) for e in model.Entry) * 5)
# caegory.py:72
subcat_query = orm.select(e.category for e in model.Entry if e.visible)
If I use the implicit int PrimaryKey field on an entity class, I get errors like "type[Entry]" has no attribute "id"
. I can work around this by providing an explicit id
field but that seems like it shouldn't be necessary.
I need to figure out how to automate testing stubs. Maybe have a folder full of random implementation files, and run both mypy
and pyright
against them on a CI job?
from pony import orm
db = orm.Database()
db.bind(provider='sqlite', filename='db.sqlite')
class A(db.Entity):
name = orm.Required(str, max_len=64, index=True)
mypy
says: Name "db.Entity" is not defined
Have you considered merging these changes directly to PonyORM?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.