Skip to content

Instantly share code, notes, and snippets.

@tirkarthi
Created July 27, 2018 17:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tirkarthi/8e6e01fde74398cac7a4eb7246169956 to your computer and use it in GitHub Desktop.
Save tirkarthi/8e6e01fde74398cac7a4eb7246169956 to your computer and use it in GitHub Desktop.
from typing import List, Dict
import sys
def get_profile_a(user_id: int, likes: Dict[str, int]) -> Dict[str, int]:
return {'user_id': user_id, 'likes': len(likes)}
def get_profile_b(user_id, likes):
return {'user_id': user_id, 'likes': len(likes.keys())}
if len(sys.argv) > 1:
get_profile_a()
else:
get_profile_b()
# Without type hints
'''
➜ cpython git:(error-message-annotations) ✗ ./python foo_signature.py
Traceback (most recent call last):
File "foo_signature.py", line 13, in <module>
get_profile_b()
TypeError: get_profile_b() missing 2 required positional arguments: 'user_id' and 'likes'
'''
# With type hints
'''
➜ cpython git:(error-message-annotations) ✗ ./python foo_signature.py types
Traceback (most recent call last):
File "foo_signature.py", line 11, in <module>
get_profile_a()
TypeError: get_profile_a() missing 2 required positional arguments: 'user_id' and 'likes'
annotation: (user_id: <class 'int'>, likes: typing.Dict[str, int], return: typing.Dict[str, int])
'''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment