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

Reenable mypy type checking #1600

Open
wants to merge 1 commit into
base: main
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
2 changes: 1 addition & 1 deletion nbgrader/alembic/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

# Interpret the config file for Python logging.
# This line sets up loggers basically.
fileConfig(config.config_file_name)
fileConfig(config.config_file_name) # type: ignore[arg-type]

# add your model's MetaData object here
# for 'autogenerate' support
Expand Down
72 changes: 33 additions & 39 deletions nbgrader/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class MissingEntry(ValueError):
pass


class Assignment(Base):
class Assignment(Base): # type: ignore
"""Database representation of the master/source version of an assignment."""

__tablename__ = "assignment"
Expand Down Expand Up @@ -116,7 +116,7 @@ def __repr__(self):
return "Assignment<{}>".format(self.name)


class Notebook(Base):
class Notebook(Base): # type: ignore
"""Database representation of the master/source version of a notebook."""

__tablename__ = "notebook"
Expand Down Expand Up @@ -212,7 +212,7 @@ def __repr__(self):
return "Notebook<{}/{}>".format(self.assignment.name, self.name)


class BaseCell(Base):
class BaseCell(Base): # type: ignore
"""Database representation of a cell. It is meant as a base class for cells where additional behavior is added through mixin classes."""

__tablename__ = "base_cell"
Expand Down Expand Up @@ -370,7 +370,7 @@ def __repr__(self):
__mapper_args__ = {'polymorphic_identity': 'TaskCell'}


class SourceCell(Base):
class SourceCell(Base): # type: ignore
__tablename__ = "source_cell"
__table_args__ = (UniqueConstraint('name', 'notebook_id'),)

Expand Down Expand Up @@ -428,7 +428,7 @@ def __repr__(self):
self.assignment.name, self.notebook.name, self.name)


class Student(Base):
class Student(Base): # type: ignore
"""Database representation of a student."""

__tablename__ = "student"
Expand Down Expand Up @@ -484,7 +484,7 @@ def __repr__(self):
return "Student<{}>".format(self.id)


class SubmittedAssignment(Base):
class SubmittedAssignment(Base): # type: ignore
"""Database representation of an assignment submitted by a student."""

__tablename__ = "submitted_assignment"
Expand Down Expand Up @@ -621,7 +621,7 @@ def __repr__(self) -> str:
return "SubmittedAssignment<{} for {}>".format(self.name, self.student.id)


class SubmittedNotebook(Base):
class SubmittedNotebook(Base): # type: ignore
"""Database representation of a notebook submitted by a student."""

__tablename__ = "submitted_notebook"
Expand Down Expand Up @@ -733,7 +733,7 @@ def __repr__(self):
self.assignment.name, self.name, self.student.id)


class Grade(Base):
class Grade(Base): # type: ignore
"""Database representation of a grade assigned to the submitted version of
a grade cell.

Expand Down Expand Up @@ -803,13 +803,7 @@ class Grade(Base):
#: :class:`~nbgrader.api.GradeCell`
max_score_gradecell = None
max_score_taskcell = None

@property
def max_score(self):
if self.max_score_taskcell:
return self.max_score_taskcell
else:
return self.max_score_gradecell
max_score = None

#: Whether the autograded score is a result of failed autograder tests. This
#: is True if the autograder score is zero and the cell type is "code", and
Expand Down Expand Up @@ -844,7 +838,7 @@ def __repr__(self):
self.assignment.name, self.notebook.name, self.name, self.student.id)


class Comment(Base):
class Comment(Base): # type: ignore
"""Database representation of a comment on a cell in a submitted notebook."""

__tablename__ = "comment"
Expand Down Expand Up @@ -920,7 +914,7 @@ def __repr__(self):
return "Comment<{}/{}/{} for {}>".format(
self.assignment.name, self.notebook.name, self.name, self.student.id)

class Course(Base):
class Course(Base): # type: ignore
"""Table to store the courses"""

__tablename__ = "course"
Expand Down Expand Up @@ -1361,7 +1355,7 @@ def close(self):
self.db.remove()
self.engine.dispose()

def check_course(self, course_id: str = "default_course", **kwargs: dict) -> Course:
def check_course(self, course_id: str = "default_course", **kwargs: Any) -> Optional[Course]:
"""Set the course id

Parameters
Expand Down Expand Up @@ -1404,7 +1398,7 @@ def students(self) -> List[Student]:
.order_by(Student.last_name, Student.first_name)\
.all()

def add_student(self, student_id: str, **kwargs: dict) -> Student:
def add_student(self, student_id: str, **kwargs: Any) -> Student:
"""Add a new student to the database.

Parameters
Expand Down Expand Up @@ -1456,7 +1450,7 @@ def find_student(self, student_id: str) -> Student:

return student

def update_or_create_student(self, student_id: str, **kwargs: dict) -> Student:
def update_or_create_student(self, student_id: str, **kwargs: Any) -> Student:
"""Update an existing student, or create it if it doesn't exist.

Parameters
Expand Down Expand Up @@ -1531,7 +1525,7 @@ def assignments(self) -> List[Assignment]:
.order_by(Assignment.duedate, Assignment.name)\
.all()

def add_assignment(self, name: str, **kwargs: dict) -> Assignment:
def add_assignment(self, name: str, **kwargs: Any) -> Assignment:
"""Add a new assignment to the gradebook.

Parameters
Expand All @@ -1547,9 +1541,9 @@ def add_assignment(self, name: str, **kwargs: dict) -> Assignment:

"""
if 'duedate' in kwargs:
kwargs['duedate'] = utils.parse_utc(kwargs['duedate'])
kwargs['duedate'] = utils.parse_utc(kwargs['duedate']) # type: ignore[assignment,arg-type]
if 'course_id' not in kwargs:
kwargs['course_id'] = self.course_id
kwargs['course_id'] = self.course_id # type: ignore[assignment]
assignment = Assignment(name=name, **kwargs)
self.db.add(assignment)
try:
Expand Down Expand Up @@ -1583,7 +1577,7 @@ def find_assignment(self, name: str) -> Assignment:

return assignment

def update_or_create_assignment(self, name: str, **kwargs: dict) -> Assignment:
def update_or_create_assignment(self, name: str, **kwargs: Any) -> Assignment:
"""Update an existing assignment, or create it if it doesn't exist.

Parameters
Expand All @@ -1606,7 +1600,7 @@ def update_or_create_assignment(self, name: str, **kwargs: dict) -> Assignment:
else:
for attr in kwargs:
if attr == 'duedate':
setattr(assignment, attr, utils.parse_utc(kwargs[attr]))
setattr(assignment, attr, utils.parse_utc(kwargs[attr])) # type: ignore[arg-type]
else:
setattr(assignment, attr, kwargs[attr])
try:
Expand Down Expand Up @@ -1647,7 +1641,7 @@ def remove_assignment(self, name):

# Notebooks

def add_notebook(self, name: str, assignment: str, **kwargs: dict) -> Notebook:
def add_notebook(self, name: str, assignment: str, **kwargs: Any) -> Notebook:
"""Add a new notebook to an assignment.

Parameters
Expand Down Expand Up @@ -1772,7 +1766,7 @@ def remove_notebook(self, name, assignment):

# Grade cells

def add_grade_cell(self, name: str, notebook: str, assignment: str, **kwargs: dict) -> GradeCell:
def add_grade_cell(self, name: str, notebook: str, assignment: str, **kwargs: Any) -> GradeCell:
"""Add a new grade cell to an existing notebook of an existing
assignment.

Expand Down Expand Up @@ -1878,7 +1872,7 @@ def find_graded_cell(self, name: str, notebook: str, assignment: str) -> Union[G

return grade_cell

def update_or_create_grade_cell(self, name: str, notebook: str, assignment: str, **kwargs: dict) -> GradeCell:
def update_or_create_grade_cell(self, name: str, notebook: str, assignment: str, **kwargs: Any) -> GradeCell:
"""Update an existing grade cell in a notebook of an assignment, or
create the grade cell if it does not exist.

Expand Down Expand Up @@ -1917,7 +1911,7 @@ def update_or_create_grade_cell(self, name: str, notebook: str, assignment: str,

# Solution cells

def add_solution_cell(self, name: str, notebook: str, assignment: str, **kwargs: dict) -> SolutionCell:
def add_solution_cell(self, name: str, notebook: str, assignment: str, **kwargs: Any) -> SolutionCell:
"""Add a new solution cell to an existing notebook of an existing
assignment.

Expand Down Expand Up @@ -1983,7 +1977,7 @@ def update_or_create_solution_cell(self,
name: str,
notebook: str,
assignment: str,
**kwargs: dict
**kwargs: Any
) -> SolutionCell:
"""Update an existing solution cell in a notebook of an assignment, or
create the solution cell if it does not exist.
Expand Down Expand Up @@ -2023,7 +2017,7 @@ def update_or_create_solution_cell(self,

# Task cells

def add_task_cell(self, name: str, notebook: str, assignment: str, **kwargs: dict) -> TaskCell:
def add_task_cell(self, name: str, notebook: str, assignment: str, **kwargs: Any) -> TaskCell:
"""Add a new task cell to an existing notebook of an existing
assignment.

Expand Down Expand Up @@ -2124,7 +2118,7 @@ def update_or_create_task_cell(self, name, notebook, assignment, **kwargs):

# Source cells

def add_source_cell(self, name: str, notebook: str, assignment: str, **kwargs: dict) -> SourceCell:
def add_source_cell(self, name: str, notebook: str, assignment: str, **kwargs: Any) -> SourceCell:
"""Add a new source cell to an existing notebook of an existing
assignment.

Expand Down Expand Up @@ -2186,7 +2180,7 @@ def find_source_cell(self, name: str, notebook: str, assignment: str) -> SourceC

return source_cell

def update_or_create_source_cell(self, name: str, notebook: str, assignment: str, **kwargs: dict) -> SourceCell:
def update_or_create_source_cell(self, name: str, notebook: str, assignment: str, **kwargs: Any) -> SourceCell:
"""Update an existing source cell in a notebook of an assignment, or
create the source cell if it does not exist.

Expand Down Expand Up @@ -2225,7 +2219,7 @@ def update_or_create_source_cell(self, name: str, notebook: str, assignment: str

# Submissions

def add_submission(self, assignment: str, student: str, **kwargs: dict) -> SubmittedAssignment:
def add_submission(self, assignment: str, student: str, **kwargs: Any) -> SubmittedAssignment:
"""Add a new submission of an assignment by a student.

This method not only creates the high-level submission object, but also
Expand All @@ -2249,7 +2243,7 @@ def add_submission(self, assignment: str, student: str, **kwargs: dict) -> Submi
"""

if 'timestamp' in kwargs:
kwargs['timestamp'] = utils.parse_utc(kwargs['timestamp'])
kwargs['timestamp'] = utils.parse_utc(kwargs['timestamp']) # type: ignore[assignment,arg-type]

try:
submission = SubmittedAssignment(
Expand Down Expand Up @@ -2308,7 +2302,7 @@ def find_submission(self, assignment: str, student: str) -> SubmittedAssignment:

return submission

def update_or_create_submission(self, assignment: str, student: str, **kwargs: dict) -> SubmittedAssignment:
def update_or_create_submission(self, assignment: str, student: str, **kwargs: Any) -> SubmittedAssignment:
"""Update an existing submission of an assignment by a given student,
or create a new submission if it doesn't exist.

Expand Down Expand Up @@ -2337,7 +2331,7 @@ def update_or_create_submission(self, assignment: str, student: str, **kwargs: d
else:
for attr in kwargs:
if attr == 'timestamp':
setattr(submission, attr, utils.parse_utc(kwargs[attr]))
setattr(submission, attr, utils.parse_utc(kwargs[attr])) # type: ignore[arg-type]
else:
setattr(submission, attr, kwargs[attr])
try:
Expand Down Expand Up @@ -2527,7 +2521,7 @@ def find_submission_notebook(self, notebook: str, assignment: str, student: str)
"""

try:
notebook = self.db.query(SubmittedNotebook)\
notebook_db = self.db.query(SubmittedNotebook)\
.join(Notebook, Notebook.id == SubmittedNotebook.notebook_id)\
.join(SubmittedAssignment, SubmittedAssignment.id == SubmittedNotebook.assignment_id)\
.join(Assignment, Assignment.id == SubmittedAssignment.assignment_id)\
Expand All @@ -2541,7 +2535,7 @@ def find_submission_notebook(self, notebook: str, assignment: str, student: str)
raise MissingEntry("No such submitted notebook: {}/{} for {}".format(
assignment, notebook, student))

return notebook
return notebook_db

def find_submission_notebook_by_id(self, notebook_id):
"""Find a submitted notebook by its unique id.
Expand Down
5 changes: 3 additions & 2 deletions nbgrader/apps/autogradeapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,20 @@
from traitlets import default

from .baseapp import NbGrader, nbgrader_aliases, nbgrader_flags
from .baseapp import NbGraderAliasesType, NbGraderFlagsType
from ..converters import BaseConverter, Autograde, NbGraderException
from traitlets.traitlets import MetaHasTraits
from traitlets.config.loader import Config
from typing import List

aliases = {
aliases: NbGraderAliasesType = {
'course': 'CourseDirectory.course_id'
}
aliases.update(nbgrader_aliases)
aliases.update({
})

flags = {}
flags: NbGraderFlagsType = {}
flags.update(nbgrader_flags)
flags.update({
'create': (
Expand Down
13 changes: 7 additions & 6 deletions nbgrader/apps/baseapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import traceback
import logging
import traitlets.log
import typing

from jupyter_core.application import JupyterApp
from textwrap import dedent
Expand All @@ -26,18 +27,18 @@
from traitlets.traitlets import MetaHasTraits
from typing import List as TypingList
from io import StringIO
from typing import Any


nbgrader_aliases = {
NbGraderAliasesType = typing.Dict[str, typing.Any]
nbgrader_aliases: NbGraderAliasesType = {
'log-level' : 'Application.log_level',
'student': 'CourseDirectory.student_id',
'assignment': 'CourseDirectory.assignment_id',
'notebook': 'CourseDirectory.notebook_id',
'db': 'CourseDirectory.db_url',
'course-dir': 'CourseDirectory.root'
}
nbgrader_flags = {
NbGraderFlagsType = typing.Dict[str, typing.Tuple[typing.Dict[str, typing.Dict[str, typing.Any]], str]]
nbgrader_flags: NbGraderFlagsType = {
'debug': (
{'Application' : {'log_level' : 'DEBUG'}},
"set log level to DEBUG (maximize logging output)"
Expand Down Expand Up @@ -182,7 +183,7 @@ def all_configurable_classes(self) -> TypingList[MetaHasTraits]:
def _config_file_name_default(self) -> str:
return u'nbgrader_config'

def _load_config(self, cfg: Config, **kwargs: Any) -> None:
def _load_config(self, cfg: Config, **kwargs: typing.Any) -> None:
if 'NbGraderConfig' in cfg:
self.log.warning(
"Use NbGrader in config, not NbGraderConfig. Outdated config:\n%s",
Expand Down Expand Up @@ -342,7 +343,7 @@ def print_subcommands(self):
for key, (app, desc) in self.subcommands.items():
print(" {}\n{}\n".format(key, desc))

def load_config_file(self, **kwargs: Any) -> None:
def load_config_file(self, **kwargs):
"""Load the config file.
By default, errors in loading config are handled, and a warning
printed on screen. For testing, the suppress_errors option is set
Expand Down
5 changes: 3 additions & 2 deletions nbgrader/apps/collectapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@
from traitlets import default

from .baseapp import NbGrader, nbgrader_aliases, nbgrader_flags
from .baseapp import NbGraderAliasesType, NbGraderFlagsType
from ..exchange import Exchange, ExchangeCollect, ExchangeError


aliases = {}
aliases: NbGraderAliasesType = {}
aliases.update(nbgrader_aliases)
aliases.update({
"timezone": "Exchange.timezone",
"course": "CourseDirectory.course_id",
})

flags = {}
flags: NbGraderFlagsType = {}
flags.update(nbgrader_flags)
flags.update({
'update': (
Expand Down
Loading