Type hints, PEP 561, and mypy-django

2018-07-22 Thread Luke Murphy

Hi folks,

Following a short discussion over at:


https://github.com/machinalis/mypy-django/issues/9,


There is some renewed interest in getting stuck into type hints for
Django. I wanted to drop a mail here to see what the status is.

In preparation for this email, I've been combing the Django issue
tracker and some related project Github issues to see what has already
been done and adding notes over at:


https://github.com/lwm/mypy-django/issues/1


Was I correct in discovering that there is some general consensus on
waiting PEP 561 to land? It seems like that PEP has been accepted and
there are three options for implementation:


PEP 561 notes three main ways to distribute type information. The
first is a package that has only inline type annotations in the code
itself. The second is a package that ships stub files with type
information alongside the runtime code. The third method, also known
as a “stub only package” is a package that ships type information for
a package separately as stub files.


I saw mention of the necessity of DEP being submitted.  Has it been
done yet or started? I can't see anything related over at:


https://github.com/django/deps


Hope someone can enlighten me, thanks!

Best,

Luke

--
You received this message because you are subscribed to the Google Groups "Django 
developers  (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-developers+unsubscr...@googlegroups.com.
To post to this group, send email to django-developers@googlegroups.com.
Visit this group at https://groups.google.com/group/django-developers.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-developers/20180722111916.xvbjtbxs7d2zu6jy%40lostatsea.lostatsea.
For more options, visit https://groups.google.com/d/optout.


Re: python manage.py inspectdb makes all fields with a default value blank=True, null=True

2018-07-22 Thread Collin Anderson
It's tricky to automatically convert the database default value to python,
but I wonder if it would help to put the default value in a python comment
at the end of the field? like:

models.IntegerField(blank=True, null=True)  # DB Default value: 1

That way it's more clear in models.py what's going on in the database, and
you can more easily change the model yourself if you want.

On Fri, Jul 20, 2018 at 5:31 PM Kimball Leavitt 
wrote:

> I opened a ticket for it here -->
> https://code.djangoproject.com/ticket/29583
>
> On Friday, July 20, 2018 at 3:27:33 PM UTC-6, Kimball Leavitt wrote:
>>
>> Note: I posted this on django-users
>> 
>>  and
>> was told to come here.
>>
>> I'm not sure if this is a bug, feature request, or something else.
>>
>> Summary: *python manage.py inspectdb* makes all fields with a default
>> value *blank=True, null=True*
>>
>> I'm using:
>>
>>- Ubuntu 16.04
>>- Python 3.7.0
>>- Django 2.0.4
>>- Mariadb 10.2.16
>>
>>
>> Before I get into this, I realize that
>>
>>- "Django is best suited for developing new applications"
>>- This probably isn't a huge deal or a high priority
>>
>> ...BUT I figured I'd submit this anyway while I was thinking about it.
>> Maybe it'll save someone some extra work down the line.
>>
>>
>> Steps to reproduce:
>>
>>
>>- Setup a legacy db in *settings.py* (see
>>https://docs.djangoproject.com/en/2.0/howto/legacy-databases/)
>>   - Prerequisite: you have a column in one of your tables that has a
>>   default value.
>>   - Example: *test_field int(11) *DEFAULT 1
>>   - Run *python manage.py inspectdb [--database DATABASE_NAME  [
>>TABLE ] ] > models.py*
>>- Open *models.py*
>>   - If you don't include *> models.py* you can see the output from
>>   the commandline
>>- *test_field* will look something like this:
>>   - *test_field* = models.IntegerField(*blank=True, null=True*)
>>   - What I would expect:
>>   - *test_field* = models.IntegerField(*default=1*)
>>
>>
>> What I've been able to dig up:
>>
>> # from inspectdb.py
>> 
>>  (line
>> 144, comments included in the original file)
>>
>> *142  # Add 'null' and 'blank', if the 'null_ok' flag was present in the*
>> *143  # table description.*
>> *144  if row[6]:  # If it's NULL...*
>> *145  extra_params['blank'] = True*
>> *146  extra_params['null'] = True*
>>
>>
>> # from introspection.py
>> 
>>  in *get_field_description *(line 95, comment is mine)
>>
>>
>> *86  fields = []*
>> *87  for line in cursor.description:*
>> *88 info = field_info[line[0]]*
>> *89 fields.append(FieldInfo(*
>> *90   *line[:3],*
>> *91**  to_int(info.max_len) or line[3],*
>> *92  to_int(info.num_prec) or line[4],*
>> *93  to_int(info.num_scale) or line[5],*
>> *94  line[6],*
>> *95  info.column_default, # THIS LINE IS JUST WHATEVER THE
>> COLUMN DEFAULT IS*
>> *96  info.extra,*
>> *97  info.is_unsigned,*
>> *98  ))*
>> *99  return fields*
>>
>>
>> I'm sure there are a lot of things I don't understand about this, but I
>> don't think that having a default value should automatically add *blank=True,
>> null=True. *
>> *Is having a default value really a "'null_ok' flag"?*
>>
>> I think it should only put *blank=True, null=True* if the default is set
>> to *NULL* in the column. Otherwise, it should set the default to
>> whatever the default value is (ex: *default=1*)
>>
>> Caveat: I've only tested this with int fields.
>>
> --
> You received this message because you are subscribed to the Google Groups
> "Django developers (Contributions to Django itself)" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-developers+unsubscr...@googlegroups.com.
> To post to this group, send email to django-developers@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-developers.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-developers/a6aedffd-a04b-4c38-8218-850aa3394364%40googlegroups.com
> 
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers  (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-developers+unsubscr...@googlegroups.com.
To post to this group, send email to django-developer