Re: [Django] #35641: Pattern matching on model objects

2024-07-30 Thread Django
#35641: Pattern matching on model objects
-+-
 Reporter:  jdahlin  |Owner:  (none)
 Type:  New feature  |   Status:  new
Component:  Database layer   |  Version:  5.0
  (models, ORM)  |
 Severity:  Normal   |   Resolution:
 Keywords:   | Triage Stage:
 |  Unreviewed
Has patch:  0|  Needs documentation:  0
  Needs tests:  0|  Patch needs improvement:  0
Easy pickings:  0|UI/UX:  0
-+-
Comment (by jdahlin):

 Sorry, I got confused, __match_args__ needs to be a tuple and cannot be a
 property, so my suggested implementation above is too simplistic. I real
 one would have to be a bit more involved and generate the values and
 create a tuple inside the models metaclass.
-- 
Ticket URL: 
Django 
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/0107019102d3980f-544508f0-2229-421b-bfd6-89e5072945c6-00%40eu-central-1.amazonses.com.


Re: [Django] #35641: Pattern matching on model objects

2024-07-30 Thread Django
#35641: Pattern matching on model objects
-+-
 Reporter:  jdahlin  |Owner:  (none)
 Type:  New feature  |   Status:  new
Component:  Database layer   |  Version:  5.0
  (models, ORM)  |
 Severity:  Normal   |   Resolution:
 Keywords:   | Triage Stage:
 |  Unreviewed
Has patch:  0|  Needs documentation:  0
  Needs tests:  0|  Patch needs improvement:  0
Easy pickings:  0|UI/UX:  0
-+-
Description changed by jdahlin:

Old description:

> It would be nice if I could use pattern matching on Django model fields,
> this is especially important in context where I don't know what the model
> is, for example in a global post_save() handler where I want to do some
> specific logic based on a subclass.
>

> {{{#!python
> class Person(Model):
> name = TextField(...)
> age = IntegerField(...)
>
> class Company(Model):
> address = TextField(...)
> }}}
>
> I would then be able to do
> {{{#!python
> @receiver(pre_save)
> def pre_save(sender, instance, ):
># model comes in via signals or some other way, we don't know which
> subclass it is
>match model:
>   # equivalent to isinstance(model, Person) and model.age > 10:
>case Person(age) if age> 10:
> 
>   # equivalent to isinstance(model, Company) and
> model.name.endswith('Street')
>case Company(address) if name.endswith('Street'):
> 
># or just extract and return a value
>case Person(name):
>return name
> }}}
>
> The implementation for this is fairly straight-forward, add this to
> models.Model.
>
> {{{#!python
> @property
> def __match_args__(self) -> tuple[str, ...]:
> return tuple(f.name for f in self._meta.get_fields())
> }}}
>
> Happy to create a PR if this is interesting.

New description:

 It would be nice if I could use pattern matching on Django model fields,
 this is especially important in context where I don't know what the model
 is, for example in a global post_save() handler where I want to do some
 specific logic based on a subclass.


 {{{#!python
 class Person(Model):
 name = TextField(...)
 age = IntegerField(...)

 class Company(Model):
 address = TextField(...)
 }}}

 I would then be able to do
 {{{#!python
 @receiver(pre_save)
 def pre_save(sender, instance, ):
# model comes in via signals or some other way, we don't know which
 subclass it is
match model:
   # equivalent to isinstance(model, Person) and model.age > 10:
case Person(age) if age> 10:
 
   # equivalent to isinstance(model, Company) and
 model.name.endswith('Street')
case Company(address) if name.endswith('Street'):
 
# or just extract and return a value
case Person(name):
return name
 }}}

 Since __match_args__ must be a tuple, the attribute must be generated
 inside a metaclass.

 ~~{{{#!python
 @property
 def __match_args__(self) -> tuple[str, ...]:
 return tuple(f.name for f in self._meta.get_fields())
 }}}~~

 Happy to create a PR if this is interesting.

--
-- 
Ticket URL: 
Django 
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/0107019102d4712d-a722e427-b172-41e9-b292-9c6e4732089b-00%40eu-central-1.amazonses.com.


Re: [Django] #35641: Pattern matching on model objects

2024-07-30 Thread Django
#35641: Pattern matching on model objects
-+-
 Reporter:  jdahlin  |Owner:  (none)
 Type:  New feature  |   Status:  new
Component:  Database layer   |  Version:  5.0
  (models, ORM)  |
 Severity:  Normal   |   Resolution:
 Keywords:   | Triage Stage:
 |  Unreviewed
Has patch:  0|  Needs documentation:  0
  Needs tests:  0|  Patch needs improvement:  0
Easy pickings:  0|UI/UX:  0
-+-
Description changed by jdahlin:

Old description:

> It would be nice if I could use pattern matching on Django model fields,
> this is especially important in context where I don't know what the model
> is, for example in a global post_save() handler where I want to do some
> specific logic based on a subclass.
>

> {{{#!python
> class Person(Model):
> name = TextField(...)
> age = IntegerField(...)
>
> class Company(Model):
> address = TextField(...)
> }}}
>
> I would then be able to do
> {{{#!python
> @receiver(pre_save)
> def pre_save(sender, instance, ):
># model comes in via signals or some other way, we don't know which
> subclass it is
>match model:
>   # equivalent to isinstance(model, Person) and model.age > 10:
>case Person(age) if age> 10:
> 
>   # equivalent to isinstance(model, Company) and
> model.name.endswith('Street')
>case Company(address) if name.endswith('Street'):
> 
># or just extract and return a value
>case Person(name):
>return name
> }}}
>
> Since __match_args__ must be a tuple, the attribute must be generated
> inside a metaclass.
>
> ~~{{{#!python
> @property
> def __match_args__(self) -> tuple[str, ...]:
> return tuple(f.name for f in self._meta.get_fields())
> }}}~~
>
> Happy to create a PR if this is interesting.

New description:

 It would be nice if I could use pattern matching on Django model fields,
 this is especially important in context where I don't know what the model
 is, for example in a global post_save() handler where I want to do some
 specific logic based on a subclass.


 {{{#!python
 class Person(Model):
 name = TextField(...)
 age = IntegerField(...)

 class Company(Model):
 address = TextField(...)
 }}}

 I would then be able to do
 {{{#!python
 @receiver(pre_save)
 def pre_save(sender, instance, ):
# model comes in via signals or some other way, we don't know which
 subclass it is
match model:
   # equivalent to isinstance(model, Person) and model.age > 10:
case Person(age) if age> 10:
 
   # equivalent to isinstance(model, Company) and
 model.name.endswith('Street')
case Company(address) if name.endswith('Street'):
 
# or just extract and return a value
case Person(name):
return name
 }}}

 Happy to create a PR if this is interesting.

--
-- 
Ticket URL: 
Django 
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/0107019102d4db0d-5bed9e90-4f6b-43ac-b68e-dddc5979fbc1-00%40eu-central-1.amazonses.com.


Re: [Django] #35641: Pattern matching on model objects

2024-07-30 Thread Django
#35641: Pattern matching on model objects
-+-
 Reporter:  Johan Dahlin |Owner:  (none)
 Type:  New feature  |   Status:  new
Component:  Database layer   |  Version:  5.0
  (models, ORM)  |
 Severity:  Normal   |   Resolution:
 Keywords:   | Triage Stage:
 |  Unreviewed
Has patch:  0|  Needs documentation:  0
  Needs tests:  0|  Patch needs improvement:  0
Easy pickings:  0|UI/UX:  0
-+-
Comment (by Simon Charette):

 I think we'd have to see a PR and how invasive it is to implement before
 taking a decision here. I personally only see marginal readability gains
 in the example you provided so I'm skeptical that's worth the complexity.
-- 
Ticket URL: 
Django 
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/0107019103c17b30-858afa20-ec91-4274-b081-2b3de92c26c9-00%40eu-central-1.amazonses.com.


Re: [Django] #35641: Pattern matching on model objects

2024-07-30 Thread Django
#35641: Pattern matching on model objects
-+-
 Reporter:  Johan Dahlin |Owner:  (none)
 Type:  New feature  |   Status:  closed
Component:  Database layer   |  Version:  5.0
  (models, ORM)  |
 Severity:  Normal   |   Resolution:  wontfix
 Keywords:   | Triage Stage:
 |  Unreviewed
Has patch:  0|  Needs documentation:  0
  Needs tests:  0|  Patch needs improvement:  0
Easy pickings:  0|UI/UX:  0
-+-
Changes (by Natalia Bidart):

 * resolution:   => wontfix
 * status:  new => closed

Comment:

 Hello Johan, welcome to the Django Community! Nice to see you again.

 I personally like your proposal, but since Django is a community driven
 project, there is a established procedure to request new features which
 requires first gathering community consensus so then a ticket can be
 accepted. To do that, please consider starting a new conversation on the
 [https://forum.djangoproject.com/c/internals/5 Django Forum], where you'll
 reach a wider audience and likely get extra feedback.

 I'll close the ticket for now, but if there is a community agreement for
 the feature request, you are welcome to come back to the ticket and point
 to the forum topic, so we can then re-open it. For more details, please
 see [https://docs.djangoproject.com/en/stable/internals/contributing/bugs-
 and-features/#requesting-features the documented guidelines for requesting
 features].

 Thanks!
-- 
Ticket URL: 
Django 
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/01070191040fd14b-8e76e51f-e7ef-4f03-9afd-1077ea56a211-00%40eu-central-1.amazonses.com.