Skip to content

Commit

Permalink
Mypy checks
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderWatzinger committed Dec 9, 2023
1 parent 2c07ca0 commit 6d7bff5
Show file tree
Hide file tree
Showing 32 changed files with 90 additions and 76 deletions.
13 changes: 9 additions & 4 deletions install/crm/cidoc_rtfs_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
#

import time
from typing import Dict, List, Optional
from typing import Any, Dict, List, Optional

import psycopg2.extras
from rdflib import URIRef
Expand All @@ -50,7 +50,7 @@
DATABASE_PASS = 'CHANGE ME'


def connect() -> psycopg2.connect:
def connect() -> Any:
return psycopg2.connect(
database=DATABASE_NAME,
user=DATABASE_USER,
Expand Down Expand Up @@ -89,11 +89,16 @@ def import_cidoc() -> None:
except Exception:
print(f'Not able to parse subject: {subject}')
continue
item = Item(code, name.replace('_', ' '), graph.comment(subject))
item = Item(
code,
name.replace('_', ' '),
graph.comment(subject)) # type: ignore

# Translations
for language in ['de', 'en', 'fr', 'ru', 'el', 'pt', 'zh']:
translation = graph.preferredLabel(subject, lang=language)
translation = graph.preferredLabel(
subject,
lang=language) # type: ignore
if translation and translation[0][1]:
item.label[language] = translation[0][1]

Expand Down
4 changes: 2 additions & 2 deletions openatlas/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import locale
from typing import Optional
from typing import Any, Optional

from flask import Flask, Response, g, request, session
from flask_babel import Babel
Expand Down Expand Up @@ -109,5 +109,5 @@ def apply_caching(response: Response) -> Response:


@app.teardown_request
def teardown_request(_exception: Optional[Exception]) -> None:
def teardown_request(_exception: Optional[Any]) -> None:
close_connection()
3 changes: 1 addition & 2 deletions openatlas/database/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ class Settings:
@staticmethod
def get_settings(
cursor: Optional[DictCursor] = None) -> dict[str, str]:
if not cursor:
cursor = g.cursor
cursor = cursor or g.cursor
cursor.execute("SELECT name, value FROM web.settings;")
return {row['name']: row['value'] for row in cursor.fetchall()}

Expand Down
4 changes: 2 additions & 2 deletions openatlas/display/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,7 @@ def uc_first(string: str) -> str:


@app.template_filter()
def display_info(data: dict[str, Union[str, list[str]]]) -> str:
def display_info(data: dict[str, Any]) -> str:
return render_template('util/info_data.html', data=data)


Expand Down Expand Up @@ -785,7 +785,7 @@ def get_iiif_file_path(id_: int) -> Path:


def convert_image_to_iiif(id_: int) -> None:
command = ["vips" if os.name == 'posix' else "vips.exe"]
command: list[Any] = ["vips" if os.name == 'posix' else "vips.exe"]
command.extend([
'tiffsave',
get_file_path(id_),
Expand Down
10 changes: 7 additions & 3 deletions openatlas/forms/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,13 @@ def process_origin(manager: Any) -> None:


def process_dates(manager: Any) -> dict[str, Any]:
data = {
'begin_from': None, 'begin_to': None, 'begin_comment': None,
'end_from': None, 'end_to': None, 'end_comment': None}
data: dict[str, Any] = {
'begin_from': None,
'begin_to': None,
'begin_comment': None,
'end_from': None,
'end_to': None,
'end_comment': None}
form = manager.form
if hasattr(form, 'begin_year_from') and form.begin_year_from.data:
data['begin_comment'] = form.begin_comment.data
Expand Down
8 changes: 4 additions & 4 deletions openatlas/forms/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@
from flask import g, request
from flask_babel import lazy_gettext as _
from flask_wtf import FlaskForm
from wtforms import MultipleFileField
from wtforms import MultipleFileField, validators

from openatlas.display.util import uc_first
from openatlas.forms.field import TreeField
from openatlas.forms.util import form_to_datetime64
from openatlas.models.entity import Entity
from openatlas.models.type import Type
from openatlas.display.util import uc_first


def file(_form: FlaskForm, field: MultipleFileField) -> None:
for file_ in request.files.getlist('file'):
if not file_ or Path(file_.filename).suffix[1:] not in \
if not file_ or Path(str(file_.filename)).suffix[1:] not in \
g.settings['file_upload_allowed_extension']:
field.errors.append(_('file type not allowed'))

Expand All @@ -26,7 +26,7 @@ def hierarchy_name_exists(form: FlaskForm, field: TreeField) -> None:
field.errors.append(_('error name exists'))


def validate(form: FlaskForm, extra_validators=None) -> bool:
def validate(form: FlaskForm, extra_validators: validators = None) -> bool:
valid = FlaskForm.validate(form)
if hasattr(form, 'begin_year_from'): # Dates
if not validate_dates(form):
Expand Down
3 changes: 2 additions & 1 deletion openatlas/models/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ def sql_export(format_: str, postfix: Optional[str] = '') -> bool:
file = app.config['EXPORT_PATH'] \
/ f'{current_date_for_filename()}_export{postfix}.{format_}'
command: Any = [
"pg_dump" if os.name == 'posix' else Path(shutil.which("pg_dump.exe"))]
"pg_dump" if os.name == 'posix'
else Path(str(shutil.which("pg_dump.exe")))]
if format_ == 'dump':
command.append('-Fc')
command.extend([
Expand Down
5 changes: 2 additions & 3 deletions openatlas/views/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,11 @@ def admin_index(
button(_('system log'), url_for('admin_log'))])
tabs['email'] = Tab(
'email',
display_info(get_form_settings(MailForm())),
display_info(get_form_settings(MailForm())) +
(display_form(form) if g.settings['mail'] else ''),
buttons=[
manual('admin/mail'),
button(_('edit'), url_for('admin_settings', category='mail'))])
if g.settings['mail']:
tabs['email'].content += display_form(form)
tabs['IIIF'] = Tab(
'IIIF',
display_info(get_form_settings(IiifForm())),
Expand Down
4 changes: 2 additions & 2 deletions openatlas/views/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ def check_update_access(entity: Entity) -> None:
abort(403)


def insert_files(manager: BaseManager) -> Union[str, Response]:
def insert_files(manager: BaseManager) -> str:
filenames = []
try:
Transaction.begin()
Expand Down Expand Up @@ -235,7 +235,7 @@ def insert_files(manager: BaseManager) -> Union[str, Response]:
return url


def save(manager: BaseManager) -> Union[str, Response]:
def save(manager: BaseManager) -> str:
Transaction.begin()
action = 'update' if manager.entity else 'insert'
try:
Expand Down
17 changes: 9 additions & 8 deletions openatlas/views/imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
from flask_wtf import FlaskForm
from werkzeug.utils import redirect, secure_filename
from werkzeug.wrappers import Response
from wtforms import BooleanField, FileField, StringField, TextAreaField
from wtforms.validators import InputRequired
from wtforms import (
BooleanField, FileField, StringField, TextAreaField, validators)

from openatlas import app
from openatlas.database.connect import Transaction
Expand All @@ -29,12 +29,12 @@ class ProjectForm(FlaskForm):
project_id: Optional[int] = None
name = StringField(
_('name'),
[InputRequired()],
[validators.InputRequired()],
render_kw={'autofocus': True})
description = TextAreaField(_('description'))
save = SubmitField(_('insert'))

def validate(self, extra_validators=None) -> bool:
def validate(self, extra_validators: validators = None) -> bool:
valid = FlaskForm.validate(self)
name = Import.get_project_by_id(self.project_id).name \
if self.project_id else ''
Expand Down Expand Up @@ -171,14 +171,14 @@ def import_project_delete(id_: int) -> Response:


class ImportForm(FlaskForm):
file = FileField(_('file'), [InputRequired()])
file = FileField(_('file'), [validators.InputRequired()])
preview = BooleanField(_('preview only'), default=True)
duplicate = BooleanField(_('check for duplicates'), default=True)
save = SubmitField(_('import'))

def validate(self, extra_validators=None) -> bool:
def validate(self, extra_validators: validators=None) -> bool:
valid = FlaskForm.validate(self)
if pathlib.Path(request.files['file'].filename) \
if pathlib.Path(str(request.files['file'].filename)) \
.suffix.lower() != '.csv':
self.file.errors.append(_('file type not allowed'))
valid = False
Expand All @@ -197,7 +197,8 @@ def import_data(project_id: int, class_: str) -> str:
class_label = g.classes[class_].label
if form.validate_on_submit():
file_ = request.files['file']
file_path = app.config['TMP_PATH'] / secure_filename(file_.filename)
file_path = \
app.config['TMP_PATH'] / secure_filename(str(file_.filename))
columns: dict[str, list[str]] = {
'allowed': ['name', 'id', 'description'],
'valid': [],
Expand Down
4 changes: 2 additions & 2 deletions openatlas/views/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from flask_wtf import FlaskForm
from werkzeug.utils import redirect
from werkzeug.wrappers import Response
from wtforms import BooleanField, PasswordField
from wtforms import BooleanField, PasswordField, validators
from wtforms.validators import InputRequired

from openatlas import app
Expand All @@ -29,7 +29,7 @@ class PasswordForm(FlaskForm):
show_passwords = BooleanField(_('show passwords'))
save = SubmitField(_('save'))

def validate(self, extra_validators=None) -> bool:
def validate(self, extra_validators: validators = None) -> bool:
valid = FlaskForm.validate(self)
hash_ = bcrypt.hashpw(
self.password_old.data.encode('utf-8'),
Expand Down
7 changes: 5 additions & 2 deletions openatlas/views/search.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
from typing import Any

from flask import g, render_template, request
from flask_babel import lazy_gettext as _
from flask_wtf import FlaskForm
from wtforms import (
BooleanField, IntegerField, SelectMultipleField, StringField, widgets)
BooleanField, IntegerField, SelectMultipleField, StringField, validators,
widgets)
from wtforms.validators import InputRequired, NoneOf, NumberRange, Optional

from openatlas import app
Expand Down Expand Up @@ -54,7 +57,7 @@ class SearchForm(FlaskForm):
validators=validator_day)
include_dateless = BooleanField(_('Include dateless entities'))

def validate(self, extra_validators=None) -> bool:
def validate(self, extra_validators: validators = None) -> bool:
valid = FlaskForm.validate(self)
from_date = form_to_datetime64(
self.begin_year.data,
Expand Down
3 changes: 2 additions & 1 deletion openatlas/views/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,8 @@ def carbon(id_: int) -> Union[str, Response]:
buttons.append(
button(_('edit'), url_for('carbon_update', id_=entity.id)))
if link_ := get_carbon_link(entity):
buttons.append(remove_link(_('radiocarbon dating'), link_, entity))
buttons.append(
str(remove_link(_('radiocarbon dating'), link_, entity)))
return render_template(
'tabs.html',
entity=entity,
Expand Down
6 changes: 3 additions & 3 deletions openatlas/views/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from werkzeug.wrappers import Response
from wtforms import (
BooleanField, HiddenField, PasswordField, SelectField, StringField,
TextAreaField)
TextAreaField, validators)
from wtforms.validators import Email, InputRequired

from openatlas import app
Expand Down Expand Up @@ -43,7 +43,7 @@ class UserForm(FlaskForm):
insert_and_continue = SubmitField(_('insert and continue'))
continue_ = HiddenField()

def validate(self, extra_validators=None) -> bool:
def validate(self, extra_validators: validators = None) -> bool:
valid = FlaskForm.validate(self)
username = ''
user_email = ''
Expand Down Expand Up @@ -110,7 +110,7 @@ def user_activity(user_id: int = 0) -> str:
entity = Entity.get_by_id(row['entity_id'])
entity_name = link(entity)
except AttributeError: # Entity already deleted
entity = None # type: ignore
entity = None
entity_name = f"id {row['entity_id']}"
user = User.get_by_id(row['user_id'])
table.rows.append([
Expand Down
2 changes: 1 addition & 1 deletion tests/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def setUp(self) -> None:
url_for('login'),
data={'username': 'Alice', 'password': 'test'})
with app.test_request_context():
app.preprocess_request() # type: ignore
app.preprocess_request()
self.alice_id = 2
self.precision_type = \
Type.get_hierarchy('External reference match')
Expand Down
4 changes: 2 additions & 2 deletions tests/test_actor.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class ActorTests(TestBaseCase):
def test_actor(self) -> None:
with app.app_context():
with app.test_request_context():
app.preprocess_request() # type: ignore
app.preprocess_request()
place = insert('place', 'Vienna')
event = insert('acquisition', 'Event Horizon')
group = insert('group', 'LV-426 colony')
Expand Down Expand Up @@ -145,7 +145,7 @@ def test_actor(self) -> None:
assert b'Ripley' in rv.data

with app.test_request_context():
app.preprocess_request() # type: ignore
app.preprocess_request()
link_ = group.get_links('P107')[0]

rv = self.app.get(
Expand Down
11 changes: 6 additions & 5 deletions tests/test_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from openatlas import app
from openatlas.database.entity import Entity
from openatlas.forms.util import form_to_datetime64
from openatlas.models.link import Link
from tests.base import TestBaseCase, get_hierarchy, insert

Expand All @@ -13,7 +14,7 @@ class AdminTests(TestBaseCase):
def test_admin(self) -> None:
with app.app_context():
with app.test_request_context():
app.preprocess_request() # type: ignore
app.preprocess_request()
person = insert('person', 'Oliver Twist')
insert('person', 'Oliver Twist')
insert('file', 'Forsaken file')
Expand Down Expand Up @@ -70,16 +71,16 @@ def test_admin(self) -> None:
assert b'An error occurred when trying to delete' in rv.data

with app.test_request_context():
app.preprocess_request() # type: ignore
app.preprocess_request()
event = insert('acquisition', 'Event Horizon')
person.update({
'attributes': {
'begin_from': '2018-01-31',
'begin_to': '2018-01-01'}})
involvement = Link.get_by_id(event.link('P11', person)[0])
involvement.begin_from = '2017-01-31'
involvement.begin_to = '2017-01-01'
involvement.end_from = '2017-01-01'
involvement.begin_from = form_to_datetime64(2017, 1, 31)
involvement.begin_to = form_to_datetime64(2017, 1, 1)
involvement.end_from = form_to_datetime64(2017, 1, 1)
involvement.update()
source = insert('source', 'Tha source')
source.link('P67', event)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def test_api(self) -> None:

with app.app_context():
with app.test_request_context():
app.preprocess_request() # type: ignore
app.preprocess_request()
for entity in get_by_cidoc_classes(['all']):
if entity.name == 'Location of Shire':
location = entity
Expand Down
2 changes: 1 addition & 1 deletion tests/test_artifact.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class ArtifactTest(TestBaseCase):
def test_artifact(self) -> None:
with app.app_context():
with app.test_request_context():
app.preprocess_request() # type: ignore
app.preprocess_request()
source = insert('source', 'Necronomicon')
actor = insert('person', 'Conan')
place = insert('place', 'Home')
Expand Down
2 changes: 1 addition & 1 deletion tests/test_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class EventTest(TestBaseCase):
def test_event(self) -> None:
with app.app_context():
with app.test_request_context():
app.preprocess_request() # type: ignore
app.preprocess_request()
place_name = 'Lewis and Clark'
actor_name = 'Captain Miller'
actor = insert('person', actor_name)
Expand Down
Loading

0 comments on commit 6d7bff5

Please sign in to comment.