-
Notifications
You must be signed in to change notification settings - Fork 888
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
experiment with shipping type annotations #2638
Comments
Option 1 and 3 require a release of Pyramid (con), whereas 2 does not (pro). That alone makes option 2 the most appealing to me. |
These are type annotations, not documentation. They are inherently coupled to the source of the release. I think this is not a concern. |
Are you saying that there shall be no bugs or other mistakes in the type annotations when added to the Pyramid package, or that if there are then it is no big thing? |
I think if there are bugs with them then they could simply get fixed in a later release. Again that's just my first impression as I have not used type annotations myself. |
-1 to option 2: annotations have to stay tightly in-sync with code: in fact, we should find a way to "test" them so that we don't see them drift. I see no benefit in creating an out-of-tree "authority" for them which can drift (and will, inevitably) from changes as Pyramid releases are made. |
I would love to see Pyramid type annotations, personally I like the 3rd option the most. I use PyCharm and type annotations a lot, and marking |
I would like to see the option 4: stop supporting Python 2 and use real annotations. Though a bigger problem is that nothing supports interfaces. |
Yeah |
Most of pyramid "exposed" api isn't |
I'm not sure that's true. We have two major problems in terms of typing / linting.
|
(close button is too close) |
Is there a plan to work on typing already? I don't see any mention of it in the 2.0 feature list #2362. I would love to help with stubs/comments for 1.x since I'm using it at work, but it would also be interesting to plan for Py3. |
Please when talking with pycharm folks, tell them we want to type with zope interfaces too :D |
Here is, in my eyes, what needs to happen to get typing into Pyramid. The goal would be to support typing via annotations and not via stub files or comment syntax. This is open for debate but this is the roadmap I have in my head.
If I were someone interested in doing this and starting from scratch I would probably look at what websauna is doing. It is a framework built on top of pyramid and is py3-only with annotation-based typing. |
Here it is. https://github.com/Shoobx/mypy-zope |
Hi, has anyone started working on this? If not, I'm willing to contribute, but definitely need some guidance from the maintainers. I really love Pyramid's design and architect and hate seeing it slowly becoming a relic of the past. P/s: I'm starting most of my personal projects with Pyramid, so the investment has already been made. |
I'm happy to review as best as I can - I just have limited familiarity with Python's type annotation system and haven't used things like mypy yet. |
imo the best place to start would be with webob, since |
For Pyramid itself, the biggest outstanding question to me is how to handle Configurator, especially |
You're correct that
So you define extension a: # extension a
class ConfiguratorWithExtensionA(Configurator):
def extension_a(...) -> None:
pass
def includeme(config):
config.add_directive(..., 'extension_a') Later a user could do something like: from pyramid.config import Configurator
from extension_a import ConfiguratorWithExtensionA
class MyConfigurator(Configurator, ConfiguratorWithExtensionA):
pass
config: MyConfigurator = Configurator()
with config:
config.include('extension_a')
config.extension_a(...) # <-- goal is that we can type-check this invocation correctly
app = config.make_wsgi_app() Again I'm showing how naive I am with Python types but I think the general strategy makes sense from what I've seen. |
I think this may not work, as the analysis would find that |
I think at the moment, the type checking of such dynamic objects (whose interfaces can be modified imperatively after the initial declaration, like the
So in conclusion, unless we know all the possible future combination, which basically is omniscience at this point, or willing to introduce a complicated type system, which usually is not a good idea (mandatory example: https://github.com/gruhn/typescript-sudoku), there’s no need to have the framework fully typed to a tee. TL;DR: Add typings for things we know and leave the unknown to the users. |
Yes absolutely the initial goal is just to get the core framework to have better types. The extensibility of add_directive and add_request_method can come later. A lot of core types in the framework are already defined as zope interfaces in pyramid.interfaces so ideally we’d have a way to reuse that but I don’t think z.i is expressive enough to really do what you want with a protocol in mypy. |
So, should I start with I will try to get something working soon (expecting by the end of next week). |
I’d probably suggest starting with pyramid simple because webob’s codebase is in a weird cleanup phase that needs some love so I think it’d introduce annoying stop energy to start there. |
@mmerickel Is Either way, I'll start working on |
No not a rewrite. I looked at it again and it's not as bad as I thought, things are in a pretty decent state atm. You could definitely add types to webob. MultiDict will be tricky but hey :-) Ultimately it'd be helpful because you'll want to inherit those types into pyramid.request.Request and pyramid.response.Response. |
Good point yeah I suppose a cast would be required. It feels like there's not going to be a way around that but most of my typing experience is in static typing domains (or in the overly flexible z.i per-object typing domain which won't work with python's annotations afaik). Python's annotations with mypy are in a middle ground on the spectrum that I don't fully understand but is closer to static I suppose. |
IIUC Python’s typing is static, but smart enough to support duck typing (with Protocols). |
It'd be cool if pyramid could ship type annotations for its public APIs. I talked to the pycharm folks at their booth last week and there are apparently 3 ways to do this given that we want to keep working on py2 and py3:
It would be helpful to evaluate the pros and cons of the various approaches with the primary goal being to minimize the maintenance effort required. For example, a build hook that could check that type hints were defined for public APIs.
On a side note, I think the commant syntax is ugly and that it should be considered the worst of the 3 options.
Related #2387
NOTE: This issue is vague because I only sort of know what I'm talking about.
The text was updated successfully, but these errors were encountered: