Skip to content
This repository has been archived by the owner on May 2, 2024. It is now read-only.

Use built-in generic types #130

Closed
kdmccormick opened this issue Nov 15, 2022 · 2 comments
Closed

Use built-in generic types #130

kdmccormick opened this issue Nov 15, 2022 · 2 comments
Assignees
Labels
good first issue A good task for a newcomer to start with

Comments

@kdmccormick
Copy link
Collaborator

kdmccormick commented Nov 15, 2022

Context

Before Python 3.7, in order to fully annotate collections (lists, dicts, tuples), one had to import the type names from the typing module:

import typing as t

my_numbers: t.List[int] = [1, 2, 3, 4]
my_mapping: t.Dict[str, str] = {"key": "val"}
my_coordinates: t.Set[t.Tuple[int, int]]  = {(5, 6), (8, 10), (2, -2)}

Subscripting the built-in types directly (e.g., list[int]) would result in a syntax error like TypeError: 'type' object is not subscriptable. This is unfortunate because Tutor thoroughly annotates all types, including nested data structures, which are harder to read when using the type names from typing.

After Python 3.7, though, the built-in collection types can be used directly:

from __future__ import annotations  # <-- necessary until python 3.9

my_numbers: list[int] = [1, 2, 3, 4]
my_mapping: dict[str, str] = {"key": "val"}
my_coordinates: set[tuple[int, int]]  = {(5, 6), (8, 10), (2, -2)}

Tutor supports all non-end-of-life (EOL) Python versions. Since Python 3.6 is EOL, we can start using Python 3.7 features. Of course, we will need to retain Python 3.7 compatibility until Jun 2023 and Python 3.8 compatibility until Oct 2024, so the from __future__ import annotations line will need to stick around until then.

Acceptance Criteria

Throughout the entire Tutor codebase:

  • Add from __future__ import annotations to the top of every module, right below the module's docstring.
  • Replace any usages of t.List, t.Dict, t.Set, t.Tuple, and t.Type with their built-in equivalents: list, dict, set, tuple, and type.
  • Ensure that make test still passes under Python 3.7, 3.8 and 3.9.
@kdmccormick kdmccormick added refactor good first issue A good task for a newcomer to start with labels Nov 15, 2022
@kdmccormick kdmccormick moved this to 📋 To Do in Tutor DevEnv Adoption Nov 15, 2022
@kdmccormick
Copy link
Collaborator Author

@regisb does this look good to you?

@Carlos-Muniz
Copy link

tests/helpers.py has a function called run. It takes in an optional parameter and optionally returns using t.Optional[T].
In Python<=3.10, there is no substitute for t.Optional[T].
In 3.10+ we can change it to T|None , but we're not there yet

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
good first issue A good task for a newcomer to start with
Projects
None yet
Development

No branches or pull requests

2 participants