Giter Site home page Giter Site logo

Comments (8)

grigi avatar grigi commented on May 6, 2024 2

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.

abondar avatar abondar commented on May 6, 2024

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.

grigi avatar grigi commented on May 6, 2024

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.

abondar avatar abondar commented on May 6, 2024

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.

grigi avatar grigi commented on May 6, 2024

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.

monospacedmagic avatar monospacedmagic commented on May 6, 2024

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.

antonagestam avatar antonagestam commented on May 6, 2024

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.

antonagestam avatar antonagestam commented on May 6, 2024

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.

from tortoise-orm.

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.