Comments (8)
Yes, all valid points. This is feeling more like something that we may not want to do as is in my proposal.
Technically the problem is as follows:
Lets say I define a class Stuff
withval=IntField()
It appears that for Stuff
val is Intfield
and a class variable.
But due to the metaclass rewriting the class, Stuff
val is int
and an instance variable.
In the name of being succinct (and use metaclasses as a solution) we will end up confusing anything that tries to do Type determinism.
I added a PyLint plugin so that it can stop complaining about everything, but at that time Mypy didn't have plugins yet.
I'd love a Mypy plugin 😁
from tortoise-orm.
Even though possibility is neat, I don't see any real life advantages of implementing this.
It would be harder for us to implement both syntaxes and development of standard syntax could be held down because of type annotation limitations.
And for users, having two ways to do same thing would be confusing and will most likely lead to wrong understanding of how to use Tortoise.
from tortoise-orm.
Yes, hence the "wild idea".
To get IDE's to understand how models are configured we would need to mess with typing
in any case.
Here we define python types that get mapped to DB types (Which I see how to do, sort of), our existing syntax defines DB types, that we should then map to Python types.
The big issue is they don't map cleanly. str
is unbounded so technically TextField
, but in reality it is more often a CharField
.
We need a place to put wild ideas. An issue just doesn't feel right, but I don't see where else?
from tortoise-orm.
I think issues are fine for it, but we should formulate it so, that wild ideas should come with description of a problem that we are trying to address with this wild idea. So that way, discussion on how to overcome that problem would be in place and alternative solution for that problem could be found out in issue.
Regarding typing, at the moment I don't see how we can change our types for models because of static nature of typing
module of python, while metaclasses work dynamically.
Django overcomes it with custom plugins for editors, but for Tortoise at the moment that seems like drastic overkill.
from tortoise-orm.
Yeah, I'm all for standardised solutions. e.g. when the typing
plugin system gets pinned down we can have a try. This would be especially useful as many editors have standardised on that.
We already have a PyLint
plugin that does some mapping, but that was to make PyLint
useful for Tortoise.
I wonder if one can fool the typing system so that the type of CharField(100, nullable=True)
returns a type of Optional[str]
In py3.6+ you can always define the python types yourself:
name: Optional[str] = CharField(100, nullable=True)
from tortoise-orm.
Sounds good as long as we can still have attributes that aren't fields :) I have to use 3.6+ anyway because kennethreitz/responder is 3.6+ so I personally wouldn't be affected by a version bump. Just make sure there's still a way to manually define what type of Field an attribute is, i.e. an int
could be a SmallIntField, an IntField or a BigIntField, for instance. (Not entirely sure if I understood this idea correctly. ^^' )
from tortoise-orm.
mypy seems to have a quite extensive plugin system, so something like this could probably me made to work. However, I think it would be a bad idea to give the fields types that aren't Python types. str[255]
for instance doesn't make any sense, so projects using this ORM would have to ditch the builtin types as str
would need a new definition.
If it's possible, I think it would be a better idea to tell the type system that the fields are subtypes of their respective Python types?
from tortoise-orm.
Cool! I think mypy support really would be a great feature.
The plugins for attrs and dataclasses that are shipped with mypy are probably good places to look for inspiration, and they should probably give a somewhat good indication of the amount of work that's required.
- dataclasses plugin: https://github.com/python/mypy/blob/00ccb783de8b43bcfcc22184da8846a927bcb9db/mypy/plugins/dataclasses.py
- attrs plugin: https://github.com/python/mypy/blob/00ccb783de8b43bcfcc22184da8846a927bcb9db/mypy/plugins/attrs.py
from tortoise-orm.
Related Issues (20)
- How can i print sql with register_tortoise() in fastapi?
- Default timeout unknown HOT 1
- If datetimefield is not timezone-aware get_or_create fails UNIQUE constraint. HOT 1
- Connection closes if save method was triggered in pytest.
- Can't use check field name HOT 1
- Tortoise ORM tutorial missing context? HOT 4
- Tortoise orm F expression
- Docs request: if `pk` will be unique HOT 1
- Updating config at runtime
- Meta indexes are not created safely
- mssql named instance port number
- Add support for composite primary keys and foreign keys
- Corrupt sql generated when bulk_create method is configured with on_conflict parameter
- https://tortoise.github.io/0.19.1 gives 404
- model relationship bugged HOT 8
- why are there three files for one database? HOT 1
- auto_now not working HOT 2
- Field 'DatetimeField' has error when I use '__year' or '__month'. report this:" 'int' object has no attribute 'utcoffset' " HOT 1
- Tortoise ORM integration with FastAPI lifespan ('NoneType' object is not iterable) HOT 1
- update(balance=Wallet.balance+1) how to do this? 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 tortoise-orm.