Skip to content

Introduce @remove_annotations decorator

Maurits Kienhuis requested to merge remove_annotations_decorator into master

This merge request introduces a new @remove_annotations decorator. With this decorator one can decorate a function or a class. This function / class will have all annotations removed from its own and all nested scopes for the Solution Template.

This decorator is being introduced in relation to the new typehint material for the IPP course. We are creating new exercises which will require the student to fill in typehints for otherwise complete and functioning source code. Currently this would require us to create all solution templates by hand as the generator does not support the removal of annotations as of now.

New functionality

Functions

When annotating a function all annotations will be removed from the function signature, this includes parameter typehints and the return type.

The input.

@remove_annotations
def add_two(self, y: int) -> int:
    return y

Output with function annotations removed.

def add_two(self, y):
    return y

Variables

Any annotated variables that are assigned will have their annotations removed but their values kept. If only annotation has taken place the variable will be removed.

The input.

@remove_annotations
def add_two(self, y: int) -> int:
    z: int
    z = 2
    x: int = 3
    return y + x + z

Output with all inline annotations removed.

def add_two(self, y):
    z = 2
    x = 3
    return y + x + z

Classes

When annotating a class all annotations will be removed from all child attributes and functions. This basically combines the previously shown functionality mentioned for functions and variables.

@remove_annotations
class Example:

    z: int
    assigned_z: int = 5

    def add_one(self, x: int) -> int:
        return x + 1

Output where all annotations are removed

class Example:

    assigned_z = 5

    def add_one(self, x):
        return x + 1

Possible breaking changes

  • Dependencies were updated, of which the main goal was to get libcst to a newer version.
  • Alpine no longer works reliably as a build target. Newer versions of libcst can either be build using a rust compiler or downloaded as a wheel. Alpine does not support either out of the box as its python is musl based, not glibc. Therefore the pipeline now uses bullseye.

Merge request reports

Loading