Skip to content
Foxipa
Code Completion 4 Challenges

Complete the Python Union Types Code

Practice union annotations, None as a union member, supported runtime class checks, and the parameterized-generic runtime boundary.

Intermediate ~9 min read

Challenge 1

Annotate the parameter so the function documents that it accepts either an integer or a string.

python Your answer
def normalize_code(value: ) -> str:
    return str(value).strip()

print(normalize_code(27))
print(normalize_code(" Q4 "))

Press Ctrl⌘Enter to check your answer

Challenge 2

Complete the return annotation for a lookup that can return a string or no result.

python Your answer
def lookup_status(code: int) -> :
    return "active" if code == 8 else None

print(lookup_status(8))
print(lookup_status(0))

Press Ctrl⌘Enter to check your answer

Challenge 3

Finish the runtime check to match either a bytes object or a string object.

python Your answer
def is_textual_payload(value):
    return isinstance(value, )

print(is_textual_payload(b"hi"))
print(is_textual_payload(5))

Press Ctrl⌘Enter to check your answer

Challenge 4

Complete the subclass check so that it tests whether bool belongs under int or float.

python Your answer
print(issubclass(bool, ))

Press Ctrl⌘Enter to check your answer