Skip to content

Conversation

@mkurnikov
Copy link

Here's the PR with just enough type annotations to allow mypy to run cleanly.

flake8 warnings could be fixed by updating to the latest version.

@rpkilby tox really is amazing=)

@rpkilby
Copy link
Contributor

rpkilby commented Oct 11, 2019

Thanks for throwing this together - I really appreciate it.

-rrequirements/requirements-documentation.txt

[testenv:mypy]
basepython = python3.7
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be removed

@Djailla
Copy link
Contributor

Djailla commented Oct 22, 2019

@mkurnikov flake8 has been update in by @rpkilby with my last PR on master, you can try to rebase

Copy link
Collaborator

@carltongibson carltongibson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @mkurnikov. Thanks for this.

I left a few comments inline, which are questions really.

Then:

  • Why the # type: comments, rather than annotations?
  • And, do we need these to begin? I didn't run mypy yet to see, but these are the minimum?

Thanks again!

import yaml
except ImportError:
yaml = None
yaml = None # type: ignore
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need this?

'empty': _('This selection may not be empty.')
}
default_empty_html = []
default_empty_html = [] # type: List[Any]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, so can'y mypy just infer this? Surely all lists are List[Any], like the = [] already implies that no?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like mypy can't infer the type of empty lists, so that may be why it was specified here.

https://mypy.readthedocs.io/en/latest/type_inference_and_annotations.html#explicit-types-for-collections

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To clarify, it looks like MyPy follows the "Explicit is better than implicit" portion of the Zen of Python and doesn't assume List[Any] when it's not specified and the list is empty.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure.

(Grumble, grumble... elsewhere lack of an annotation is interpreted as Any... 🙂)

child = _UnvalidatedField()
initial = {}
child = _UnvalidatedField() # type: Field
initial = {} # type: Dict[str, Any]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again here, I'm a bit like, can't we just stick with = {}. I appreciate the str restricts the keys some but... ?

media_type = None
format = None
charset = 'utf-8'
media_type = None # type: Optional[str]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there not a ? shortcut for Optional (like they have in Swift)? (Just wondering...)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I am aware, there isn't a shortcut for Optional.

@lovelydinosaur
Copy link
Contributor

I appreciate the effort, but I probably think we’re adding more noise than value here. I think type checking is great if you’re able to apply it consistently and throughout to a project from the start, but I tend to see it giving less value when backporting it onto established projects.

@carltongibson
Copy link
Collaborator

For me, it depends how this conversation goes on Django. If we go for it there then...

But the queries I have here are pertinent to that discussion. It's good to see what it'd look like.

(IRL I often end up chucking in an odd : HttpResponse or similar, even on old projects, so my editor's autocomplete will work properly...)

@kevin-brown
Copy link
Contributor

I'm generally +0 on type annotations here. I agree that there's way more value in starting off projects with them, but I also believe that strategically adding them to existing projects can be useful.

DRF still supports Python 3.5 so we can't have the clean variable type annotations that were adopted in Python 3.6. I'm willing to accept the compromise with the comment-based type annotations, even though I think they aren't as nice to look at.

@michael-k
Copy link
Contributor

I tend to see it giving less value when backporting it onto established projects.

DRF isn't just a project, it's also a library and its type annotations can be used to type check projects that use DRF. (Type annotations alone are not enough for that, but most of the work for PEP 561 compat.)

For me, it depends how this conversation goes on Django. If we go for it there then...

If someone else is looking for the current discussion, there's a Django Enhancement Proposals: django/deps#65

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants