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

feat: add type information - mypy support #1350

Closed
CoreyCole opened this issue Sep 10, 2018 · 19 comments · Fixed by #1947
Closed

feat: add type information - mypy support #1350

CoreyCole opened this issue Sep 10, 2018 · 19 comments · Fixed by #1947
Assignees
Milestone

Comments

@CoreyCole
Copy link

As of PEP 561 it is now possible for packages to provide typing information, with different options of how to do so. I'm wondering what would be the preferred course of action to include these.

It seems we could start with partial stubs where we could define things that would be most helpful/commonly used (i.e. request/response)

@CoreyCole
Copy link
Author

This is an example of a separate stubs package for numpy https://github.com/numpy/numpy-stubs

@CoreyCole
Copy link
Author

I'm still new to using mypy and I realized today we already have typing information for falcon, I just didn't know how to utilize it:

def on_post(self, req: falcon.Request, res: falcon.Response):

This gives me what I'm looking for. I'm going to leave this open to get clarification on the current state of type information in the repository to see if there is help needed anywhere for it.

@CoreyCole
Copy link
Author

CoreyCole commented Sep 12, 2018

so the current state of falcon types is that they work well for annotations, but not for running mypy. When I run mypy on my project, I get errors like

error: Cannot find module named 'falcon'

I think this is because mypy doesn't look directly in site-packages unless it finds PEP 561 implementation or stubs. I've created this falcon-stubs project using stubgen: https://github.com/CoreyCole/falcon-stubs

Putting these in my project folder, i.e. myproject/stubs/falcon and then assigning the $MYPYPATH to point to that stubs folder is solving the problem for me

@CoreyCole CoreyCole changed the title feat: add type information feat: add type information - mypy support Sep 12, 2018
@CoreyCole
Copy link
Author

Are maintainers here ok with me leaving those stubs up and open sourced for others to use/contribute to?

@rafi
Copy link

rafi commented Dec 29, 2018

Would be lovely if falcon supported PEP 561

@vytas7
Copy link
Member

vytas7 commented Dec 29, 2018

@CoreyCole Falcon is open source software with a permissive license, you are very welcome to do any derivative work 🙂

It may be interesting and useful for users incorporating mypy in their tool chain.

As for maintaining it officially with Falcon, it is always the question of cost, testing, available bandwidth etc.
Personally I think it may make even more sense for Falcon 3.0 or 3.1 when we are dropping Python 2.7 and 3.4, so the preliminary plan is we would, effectively, only support Python versions allowing type annotations natively. Then we could discuss the possibility of annotating Falcon itself without the need for stubs.

@kgriffs
Copy link
Member

kgriffs commented May 6, 2019

Now that we have dropped support for Python 2.7, let's move forward with adding type hints to Falcon's public interface directly via annotations.

@kgriffs kgriffs added enhancement good first issue Comment on this issue if you'd like to volunteer to work on this. Thanks! needs contributor Comment on this issue if you'd like to volunteer to work on this. Thanks! labels May 6, 2019
@kgriffs kgriffs added this to the Version 3.0 milestone May 6, 2019
@jxub
Copy link

jxub commented May 7, 2019

I could help with that. Do I work against the last tag (2.0.0rc4), checkout directly from master or otherwise?

@vytas7
Copy link
Member

vytas7 commented May 7, 2019

Hi @jxub !
And that sounds great!!

You check out directly from master 👍 .
The 2.x series supports Python 2, and type annotations don't work there.

@jxub
Copy link

jxub commented May 7, 2019

@vytas7 Perfect, thanks!

@jxub
Copy link

jxub commented May 8, 2019

I've created a first PR to review the API class annotations (mostly for myself) at jxub#1. I'm planning of getting rid of these type aliases on top in the api.py file ASAP. However, some types, like the handlers, have pretty complex signatures so I have to rely on the Any type as well.

@mosi-kha mosi-kha mentioned this issue Aug 6, 2019
11 tasks
@kgriffs kgriffs modified the milestones: Version 3.0, Version 3.1 Aug 28, 2019
@vytas7
Copy link
Member

vytas7 commented Feb 28, 2020

Important: it seems that Cython can now pick up and enforce those type annotations.
Which is great if it leads to an even further speed bump, but it is unfortunately not always the case, as demonstrated by @CaselIT.

We'll need to tread carefully when it comes to performance-sensitive parts, and we may need to tell Cython not to actually use those annotations in cases where they only lead to slowdowns.

@CaselIT
Copy link
Member

CaselIT commented Feb 28, 2020

Here are the relevant bit in the cython docs.
http://docs.cython.org/en/latest/src/tutorial/pure.html#static-typing

Just to specify, I've tried only a very naive test, so I don't think a meaningful conclusion could be drawn from it

@mcobzarenco
Copy link

What's the status of this issue? Does anyone have any experience using Falcon with types?

For context, I'm investigating replacing bottle in our product with Falcon. This project looks really exciting 💯, but worried about production readiness in the absence of static type checking

@Spenhouet
Copy link

@mcobzarenco My static type checker tells me:

import falcon
       ^^^^^^
       Stub file not found for "falcon"

and using falcon.Request as type annotation does not really help the static type checker.

So I guess the state is that falcon does not yet support static type checkers like MyPy or PyRight.

@d-grasshopper
Copy link

What else is expected of this ticket? Is it a matter of expanding/updating the stubs project?

@hojlind
Copy link

hojlind commented Jun 24, 2021

Is there any updates on this issue? As far as I can find, currently the only stubs available is the 3 year old ones found in the external repository. Trying to develop with type checking enabled is quite a hassle unfortunately.

@vytas7
Copy link
Member

vytas7 commented Jun 24, 2021

Hi @hojlind !
And no, unfortunately there are currently no updates on typing as we were focused on shipping 3.0, and then fixing a couple of bugs found therein as part of the 3.0.1 release.

However, now that the above has been delivered, we're working on merging the PR backlog, and brainstorming features for the 3.x series: #1894. Typing is on our radar, but we haven't decided its priority yet vs other potential improvements. (I've added it to the roadmap issue though.)

@vytas7 vytas7 mentioned this issue Jun 24, 2021
14 tasks
@hojlind
Copy link

hojlind commented Jun 24, 2021

That sounds great! I am rather new to doing larger projects in python, and equally new to how typing works in python, and what the general sentiment about adding types everywhere is. Adding stubs next to the source itself however, as suggested in #1894 sounds like a good idea, as long as the time is there for it of course :)
Am I wrong in assuming that it should be fairly straightforward, as the documentation seems to have types defined for parameters and return values everywhere? If not, perhaps I can be the next guy in line to say "i can do it".

@vytas7 vytas7 modified the milestones: Version 3.1, Version 3.2 Mar 14, 2022
@vytas7 vytas7 modified the milestones: Version 4.1, Version 4.0 Mar 30, 2022
@vytas7 vytas7 mentioned this issue May 22, 2022
12 tasks
@vytas7 vytas7 self-assigned this Aug 21, 2022
@vytas7 vytas7 added in progress and removed good first issue Comment on this issue if you'd like to volunteer to work on this. Thanks! needs contributor Comment on this issue if you'd like to volunteer to work on this. Thanks! labels Aug 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

10 participants