Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Comment or docstring? #1

Closed
gvanrossum opened this issue Oct 25, 2018 · 18 comments
Closed

Comment or docstring? #1

gvanrossum opened this issue Oct 25, 2018 · 18 comments

Comments

@gvanrossum
Copy link

The current proposal has this in a docstring. Perhaps it would be better off in a comment? IDEs tend to read the source code, so they have access to comments as well as docstrings, and the README currently appears to make runtime behavior a non-goal.

@mristin
Copy link

mristin commented Oct 26, 2018

Just my 2 cents: it makes sense to have these name-to-types integrated into the automatically generated documentation such as sphinx. As a user of a library, I'd be happy to be able to access that information.

@guettli
Copy link
Owner

guettli commented Oct 26, 2018

@mristin I have never used automatically generated documentation. I am unsure. If the name2type mapping is in the docstring, then it is visible. This means your goal is reached without any work. This speaks for docstrings.

@guettli
Copy link
Owner

guettli commented Oct 26, 2018

@gvanrossum do you have contact to PyCharm developers (I mean the people who develop PyCharm)? I am very interested in their feedback. Because that's where I would like to see the name2mapping in action.

@mristin
Copy link

mristin commented Oct 26, 2018

@guettli exactly, if it's written in the docstring, it is automatically visible in the documentation.

It makes sense to use sphinx syntax in order to get nice formatting (:...: directives, e.g. see http://www.sphinx-doc.org/en/master/usage/restructuredtext/domains.html#signatures)

@ksamuel
Copy link

ksamuel commented Oct 26, 2018

Just my 2 cents: it makes sense to have these name-to-types integrated into the automatically generated documentation such as sphinx. As a user of a library, I'd be happy to be able to access that information

Yes, but having it in a comment doesn't prevent this. You can write a plugin to that extend. After all, there is a plugin for sphinx that ignore types in the docstring, and inject type from type annotations already.

I wish we allowed typing in the docstrings since the beginning, I really do. It would have made millions of code lines in the world using sphinx automatically typed hinted without work.

But this idea has been rejected unfortunately, and we can either use annotations, or type comments.

Now, if this idea ever gets implemented and use docstrings, we will end up a really weird place where all the types cannot be in a docstring, except for this particular feature that MUST be in a docstring.

For congruence, we need to put it in a comment.

@guettli
Copy link
Owner

guettli commented Oct 26, 2018

@ksamuel sooner or later Python 2.x will die. If you use a modern Python 3.x, is it feasible to use type comments? Is there a case where annotations are not enough, and you need type comments?

@ksamuel
Copy link

ksamuel commented Oct 26, 2018 via email

@dstanek
Copy link

dstanek commented Oct 26, 2018

I like the idea of using the docstring much better. A few years ago I created dstanek/typist to use type information from docstrings. It's worked out nicely since the information is usable in our generated docs too.

@ksamuel
Copy link

ksamuel commented Oct 26, 2018 via email

@guettli
Copy link
Owner

guettli commented Oct 26, 2018

@ksamuel why should I (or someone else) convince the mypy team?

... please do not allow to have a mixed world ...

Yes, me too. I try to avoid OR in docs and APIs. Every new comer is clueless and unsure again and again. I would like to avoid this.

@ksamuel
Copy link

ksamuel commented Oct 26, 2018

Only if you want to use docstrings. If you use docstrings but don't change the status quo on type hints in doc strings, we will have a a mixed world, giving that mypy is the reference and most used implementation of type checking tooling.

@guettli
Copy link
Owner

guettli commented Oct 26, 2018

@ksamuel about mypy: Up to now the list of name2type mapping goals is small: Better type-hints in IDEs. But maybe mypy support would be nice.

I the mypy developers what they think here: python/mypy#5841

@mristin
Copy link

mristin commented Oct 26, 2018

@guettli I'd suggest name2type aims for both IDE and mypy support. As soon as you have to deal with a larger code base and a larger team/organization, I find it super useful when conventions can be enforced automatically.

I don't know if I'm leaning too much out of the window, but it might be interesting to go even beyond type checks: for example, you could use patterns for hungarian notation and enforce constraints on the type at verification time or at runtime (e.g., prefix p_.* could imply that the number is always positive).

I found the following blog post interesting: https://www.joelonsoftware.com/2005/05/11/making-wrong-code-look-wrong/
but always doubted that such an approach is viable without automatic enforcement.

@gvanrossum
Copy link
Author

@gvanrossum do you have contact to PyCharm developers (I mean the people who develop PyCharm)? I am very interested in their feedback. Because that's where I would like to see the name2mapping in action.

Try @vlasovskikh

@vlasovskikh
Copy link

vlasovskikh commented Oct 26, 2018

@guettli Personally I prefer comments, since they don't interfere with module documentation. From the implementation perspective, module docstrings are easier, since PyCharm already indexes them and you'll be able to access the docstrings from ancestor __init__.py files quickly. As for module-level comments, it is possible to add a custom indexer for these mappings. The type inference logic could be implemented as a plugin for PyCharm that adds a custom type provider to our type inference.

@guettli
Copy link
Owner

guettli commented Oct 28, 2018

@vlasovskikh thank you for your feedback. By the way I updated the README and added an argument against name2type mapping. I would like to know what you think about this:

It is not needed since stats are enough. Example: If you code base contains a variable name "request" 100 times, and in 70 times the variable type is an instance of "django.http.HttpRequest" (detected by usual type-annotation), then it is redundant to specify this explicit in a file. The IDE (or code quality checking tool) could gather this information by looking at the stats.

@guettli
Copy link
Owner

guettli commented Nov 6, 2018

Just for the records. I asked in the PyCharm issue tracker, but received no reply up to now:
https://youtrack.jetbrains.com/issue/PY-32490

@guettli
Copy link
Owner

guettli commented Nov 8, 2018

I received feedback from PyCharm:

At the moment, we don't plan to support the proposal you've mentioned. It could be implemented as a third-party plugin for PyCharm

Source: https://youtrack.jetbrains.com/issue/PY-32490#focus=streamItem-27-3142017-0-0

Since my proposal won't reach my goal (integration in PyCharm) my motivation is gone.

Thank you all who participated in this idea and gave feedback.

I think it was useful to discuss this, even if it was not successful.

Have a nice time, enjoy live, au revoir.

I leave this open for now, and will close this in a month.

@guettli guettli closed this as completed Dec 10, 2018
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

No branches or pull requests

6 participants