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

Typing requires template globals to accept no arguments #4098

Closed
lucaswerkmeister opened this issue May 22, 2021 · 5 comments · Fixed by #4109
Closed

Typing requires template globals to accept no arguments #4098

lucaswerkmeister opened this issue May 22, 2021 · 5 comments · Fixed by #4109
Labels

Comments

@lucaswerkmeister
Copy link
Contributor

The typings in Flask 2.0.1 require a function decorated with @app.template_global() to take no arguments. As far as I’m aware, template globals certainly are supposed to take arguments (e.g. the example in the API documentation takes one argument), so I assume this is not intended. I think this specific issue was introduced with #4071, which applied the TemplateGlobalCallable type alias to that decorator; however, the TemplateGlobalCallable type itself is already an alias for t.Callable[[], t.Any] (i.e., take no arguments and return anything) in Flask 2.0.0. I haven’t checked yet if app.add_template_global() is affected.

Example code:

import flask


app = flask.Flask(__name__)


@app.template_global()
def identity(arg: str) -> str:
    return arg

app.py:7: error: Argument 1 has incompatible type "Callable[[str], str]"; expected "Callable[[], Any]"
Found 1 error in 1 file (checked 1 source file)

Sample repository: https://github.com/lucaswerkmeister/repro-flask-mypy-template_global

Steps to reproduce:

git clone https://github.com/lucaswerkmeister/repro-flask-mypy-template_global
make -C repro-flask-mypy-template_global

Environment:

  • Python version: 3.9.5
  • Flask version: 2.0.1
  • Mypy version: 0.812
@davidism davidism changed the title Typing in 2.0.1 requires template globals to accept no arguments Typing requires template globals to accept no arguments May 22, 2021
lucaswerkmeister added a commit to lucaswerkmeister/tool-ranker that referenced this issue May 23, 2021
@ajayd-san
Copy link
Contributor

hey, can i have this issue?
I'm quite new to open source so i might need additional pointers on where to get started apart from all the contributing guidelines which I already have an idea about 😅

@Yourun-proger
Copy link
Contributor

@default-303, hello! I am not the maintainer of this repo, but I can tell you a few things about pull requests. Don't make my mistakes - don't try frantically looking at what you don't like in the code or when you see an issue to do something right there (I'm sorry that I did this Pallets again, sorry ...) If we talk about issues in general, you need to carefully consider the problem, look at the specific places in the code where the author sees flaws. If there is something unknown to you from the world of programming - delve into it too.
P.S.
This is how I now analyze this problem.

@Yourun-proger
Copy link
Contributor

Analyze! Think! Find a solution! Write the code! Good luck, @default-303)

@ajayd-san
Copy link
Contributor

@Yourun-proger Yes I will, thanks https://github.com/default-303/flask.git

@lucaswerkmeister
Copy link
Contributor Author

Seems to be fixed:

$ git clone https://github.com/lucaswerkmeister/repro-flask-mypy-template_global
Cloning into 'repro-flask-mypy-template_global'...
remote: Enumerating objects: 6, done.
remote: Counting objects: 100% (6/6), done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 6 (delta 0), reused 6 (delta 0), pack-reused 0
Receiving objects: 100% (6/6), done.
$ sed -i '/flask/ s/^.*$/git+git:\/\/github.com\/pallets\/flask@aac67289e517bda4ef488819edab74539ef8e9b9#egg=flask/' repro-flask-mypy-template_global/requirements.txt
$ make -C repro-flask-mypy-template_global/
make: Entering directory '/tmp/repro-flask-mypy-template_global'
python3 -m venv venv/ && \
. venv/bin/activate && \
pip install --upgrade pip && \
pip install -r requirements.txt && \
mypy app.py
Requirement already satisfied: pip in ./venv/lib/python3.9/site-packages (21.1.1)
Collecting pip
  Using cached pip-21.1.2-py3-none-any.whl (1.5 MB)
Installing collected packages: pip
  Attempting uninstall: pip
    Found existing installation: pip 21.1.1
    Uninstalling pip-21.1.1:
      Successfully uninstalled pip-21.1.1
Successfully installed pip-21.1.2
Collecting flask
  Cloning git://github.com/pallets/flask (to revision aac67289e517bda4ef488819edab74539ef8e9b9) to /tmp/pip-install-p37w2zmc/flask_af79151c67bc4a58880143fc4a889f2b
  Running command git clone -q git://github.com/pallets/flask /tmp/pip-install-p37w2zmc/flask_af79151c67bc4a58880143fc4a889f2b
  Running command git rev-parse -q --verify 'sha^aac67289e517bda4ef488819edab74539ef8e9b9'
  Running command git fetch -q git://github.com/pallets/flask aac67289e517bda4ef488819edab74539ef8e9b9
Collecting mypy==0.812
  Using cached mypy-0.812-cp39-cp39-manylinux2010_x86_64.whl (22.7 MB)
Collecting Werkzeug>=2.0
  Using cached Werkzeug-2.0.1-py3-none-any.whl (288 kB)
Collecting Jinja2>=3.0
  Using cached Jinja2-3.0.1-py3-none-any.whl (133 kB)
Collecting itsdangerous>=2.0
  Using cached itsdangerous-2.0.1-py3-none-any.whl (18 kB)
Collecting click>=8.0
  Using cached click-8.0.1-py3-none-any.whl (97 kB)
Collecting mypy-extensions<0.5.0,>=0.4.3
  Using cached mypy_extensions-0.4.3-py2.py3-none-any.whl (4.5 kB)
Collecting typing-extensions>=3.7.4
  Using cached typing_extensions-3.10.0.0-py3-none-any.whl (26 kB)
Collecting typed-ast<1.5.0,>=1.4.0
  Using cached typed_ast-1.4.3-cp39-cp39-manylinux1_x86_64.whl (769 kB)
Collecting MarkupSafe>=2.0
  Using cached MarkupSafe-2.0.1-cp39-cp39-manylinux2010_x86_64.whl (30 kB)
Using legacy 'setup.py install' for flask, since package 'wheel' is not installed.
Installing collected packages: MarkupSafe, Werkzeug, typing-extensions, typed-ast, mypy-extensions, Jinja2, itsdangerous, click, mypy, flask
    Running setup.py install for flask ... done
Successfully installed Jinja2-3.0.1 MarkupSafe-2.0.1 Werkzeug-2.0.1 click-8.0.1 flask-2.1.0.dev0 itsdangerous-2.0.1 mypy-0.812 mypy-extensions-0.4.3 typed-ast-1.4.3 typing-extensions-3.10.0.0
Success: no issues found in 1 source file
make: Leaving directory '/tmp/repro-flask-mypy-template_global'

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 18, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants