Skip to content

type hints #2654

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added examples/__init__.py
Empty file.
Empty file added examples/auth/__init__.py
Empty file.
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@
from flask_admin.theme import Bootstrap4Theme
from flask_sqlalchemy import SQLAlchemy
from flask_wtf import FlaskForm
from sqlalchemy import Column
from sqlalchemy import Integer
from sqlalchemy import String
from sqlalchemy.orm import Mapped
from sqlalchemy.orm import mapped_column
from werkzeug.security import check_password_hash
from werkzeug.security import generate_password_hash
from wtforms import fields
Expand All @@ -43,17 +42,17 @@ def index():

# inherit from flask_login.UserMixin so no need to define login methods
class User(db.Model, flask_login.UserMixin):
id: Mapped[int] = mapped_column(Integer, primary_key=True)
username: Mapped[str] = mapped_column(
id = Column(Integer, primary_key=True)
username = Column(
String(80),
unique=True,
index=True,
)
# having a _ will make the field hidden in flask-admin edit/create forms
_password: Mapped[str] = mapped_column(String(128), nullable=True)
_password = Column(String(128), nullable=True)

@property
def password(self) -> str:
def password(self):
return self._password

@password.setter
Expand All @@ -67,7 +66,7 @@ def password(self, password: str) -> None:
# on MacOS, default method="scrypt" gives AttributeError
method="pbkdf2",
)
self.alternative_id = uuid4().hex
self.alternative_id = uuid4().hex # type: ignore[assignment]
# password.setter can be called even without any user logged in, e.g.
# when creating db
if flask_login.current_user and flask_login.current_user == self:
Expand All @@ -82,9 +81,7 @@ def check_password(self, password: t.Optional[str]) -> bool:

# security: https://flask-login.readthedocs.io/en/latest/#alternative-tokens
# allows logout users
alternative_id: Mapped[str] = mapped_column(
String(32), default=uuid4().hex, index=True
)
alternative_id = Column(String(32), default=uuid4().hex, index=True)

def get_id(self):
"""
Expand Down
File renamed without changes.
Empty file.
Empty file added examples/babel/__init__.py
Empty file.
Empty file added examples/bootstrap4/__init__.py
Empty file.
File renamed without changes.
File renamed without changes.
Empty file added examples/csp_nonce/__init__.py
Empty file.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Empty file.
File renamed without changes.
File renamed without changes.
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
from flask_admin.model import typefmt
from flask_sqlalchemy import SQLAlchemy
from markupsafe import Markup
from sqlalchemy import Column
from sqlalchemy import DateTime
from sqlalchemy import Integer
from sqlalchemy import String
from sqlalchemy.orm import Mapped
from sqlalchemy.orm import mapped_column

app = Flask(__name__)
app.config["SECRET_KEY"] = "secret"
Expand Down Expand Up @@ -43,12 +43,12 @@ def set_timezone():


class Article(db.Model):
id: Mapped[int] = mapped_column(primary_key=True)
text: Mapped[str] = mapped_column(String(30))
last_edit: Mapped[datetime] = mapped_column(DateTime(timezone=True))
id = Column(Integer, primary_key=True)
text = Column(String(30))
last_edit = Column(DateTime(timezone=True))


def date_format(view, value):
def date_format(view, value, name):
"""
Ensure consistent date format and inject class for timezone.js parser.
"""
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import os
import os.path as op
from typing import Optional
from typing import Union

import jinja2.runtime
from flask import Flask
from flask import url_for
from flask_admin import Admin
Expand Down Expand Up @@ -133,7 +136,9 @@ class FileView(ModelView):


class ImageView(ModelView):
def _list_thumbnail(view, context, model, name):
def _list_thumbnail(
view, context: Optional[jinja2.runtime.Context], model, name: str
) -> Union[str, Markup]:
if not model.path:
return ""

Expand Down
File renamed without changes.
Empty file.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Empty file.
File renamed without changes.
File renamed without changes.
Empty file added examples/methodview/__init__.py
Empty file.
Empty file.
55 changes: 55 additions & 0 deletions examples/mypy-examples-config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# mypy custom settings for examples/ subfolders

[tool.mypy]
python_version = "3.9"
files = ["examples"]
show_error_codes = true
pretty = true
strict = true

# this is the difference vs pyproject.toml
# relative imports in examples cause errors when mypy is run from proj folder
ignore_missing_imports = true
disable_error_code = "name-defined" # otherwise flask_sqlalchemy db.Model needs to be ignored, littering examples
warn_unused_ignores = false # otherwise we get conflict with first run of mypy on proj folder

# Strongly recommend enabling this one as soon as you can
check_untyped_defs = false

# These shouldn't be too much additional work, but may be tricky to
# get passing if you use a lot of untyped libraries
disallow_subclassing_any = false
disallow_untyped_decorators = false
disallow_any_generics = false

# These next few are various gradations of forcing use of type annotations
disallow_untyped_calls = false
disallow_incomplete_defs = false
disallow_untyped_defs = false

# This one isn't too hard to get passing, but return on investment is lower
no_implicit_reexport = false

# This one can be tricky to get passing if you use a lot of untyped libraries
warn_return_any = false

[[tool.mypy.overrides]]
module = [
"arrow",
"azure.*",
"bson.*",
"citext",
"colour",
"flask_babel",
"flask_wtf",
"gridfs",
"marker",
"playhouse.*",
"pymongo",
"sqlalchemy.*",
"sqlalchemy_utils",
"tablib",
"wtforms.*",
"wtfpeewee.*",
]
ignore_missing_imports = true
88 changes: 0 additions & 88 deletions examples/peewee/main.py

This file was deleted.

File renamed without changes.
File renamed without changes.
Empty file.
Loading
Loading