Skip to content

Commit

Permalink
Add type hints in bridge/app.py (#575)
Browse files Browse the repository at this point in the history
* Add type hints in bridge/app.py

* Remove | Any from args and kwargs

* Add bridge to mypy config

* Add Olga in contributors

* Annotate args and kwards with Any

* Apply suggestions from code review

Co-authored-by: Ofek Lev <[email protected]>
  • Loading branch information
olgarithms and ofek authored Oct 25, 2022
1 parent b666af9 commit dcdbdf1
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 27 deletions.
55 changes: 29 additions & 26 deletions backend/src/hatchling/bridge/app.py
Original file line number Diff line number Diff line change
@@ -1,38 +1,41 @@
from __future__ import annotations

import os
import pickle
import sys
from typing import Any


class InvokedApplication:
def display_always(self, *args, **kwargs):
def display_always(self, *args: Any, **kwargs: Any) -> None:
send_app_command('display_always', *args, **kwargs)

def display_info(self, *args, **kwargs):
def display_info(self, *args: Any, **kwargs: Any) -> None:
send_app_command('display_info', *args, **kwargs)

def display_waiting(self, *args, **kwargs):
def display_waiting(self, *args: Any, **kwargs: Any) -> None:
send_app_command('display_waiting', *args, **kwargs)

def display_success(self, *args, **kwargs):
def display_success(self, *args: Any, **kwargs: Any) -> None:
send_app_command('display_success', *args, **kwargs)

def display_warning(self, *args, **kwargs):
def display_warning(self, *args: Any, **kwargs: Any) -> None:
send_app_command('display_warning', *args, **kwargs)

def display_error(self, *args, **kwargs):
def display_error(self, *args: Any, **kwargs: Any) -> None:
send_app_command('display_error', *args, **kwargs)

def display_debug(self, *args, **kwargs):
def display_debug(self, *args: Any, **kwargs: Any) -> None:
send_app_command('display_debug', *args, **kwargs)

def display_mini_header(self, *args, **kwargs):
def display_mini_header(self, *args: Any, **kwargs: Any) -> None:
send_app_command('display_mini_header', *args, **kwargs)

def abort(self, *args, **kwargs):
def abort(self, *args: Any, **kwargs: Any) -> None:
send_app_command('abort', *args, **kwargs)
sys.exit(kwargs.get('code', 1))

def get_safe_application(self):
def get_safe_application(self) -> SafeApplication:
return SafeApplication(self)


Expand All @@ -45,49 +48,49 @@ class Application:
the capabilities herein and will grant access via an attribute.
"""

def __init__(self):
def __init__(self) -> None:
self.__verbosity = int(os.environ.get('HATCH_VERBOSE', '0')) - int(os.environ.get('HATCH_QUIET', '0'))

def display_always(self, message='', **kwargs):
def display_always(self, message: str = '', **kwargs: Any) -> None:
# Do not document
print(message)

def display_info(self, message='', **kwargs):
def display_info(self, message: str = '', **kwargs: Any) -> None:
"""
Meant to be used for messages conveying basic information.
"""
if self.__verbosity >= 0:
print(message)

def display_waiting(self, message='', **kwargs):
def display_waiting(self, message: str = '', **kwargs: Any) -> None:
"""
Meant to be used for messages shown before potentially time consuming operations.
"""
if self.__verbosity >= 0:
print(message)

def display_success(self, message='', **kwargs):
def display_success(self, message: str = '', **kwargs: Any) -> None:
"""
Meant to be used for messages indicating some positive outcome.
"""
if self.__verbosity >= 0:
print(message)

def display_warning(self, message='', **kwargs):
def display_warning(self, message: str = '', **kwargs: Any) -> None:
"""
Meant to be used for messages conveying important information.
"""
if self.__verbosity >= -1:
print(message)

def display_error(self, message='', **kwargs):
def display_error(self, message: str = '', **kwargs: Any) -> None:
"""
Meant to be used for messages indicating some unrecoverable error.
"""
if self.__verbosity >= -2:
print(message)

def display_debug(self, message='', level=1, **kwargs):
def display_debug(self, message: str = '', level: int = 1, **kwargs: Any) -> None:
"""
Meant to be used for messages that are not useful for most user experiences.
The `level` option must be between 1 and 3 (inclusive).
Expand All @@ -97,11 +100,11 @@ def display_debug(self, message='', level=1, **kwargs):
elif self.__verbosity >= level:
print(message)

def display_mini_header(self, message='', **kwargs):
def display_mini_header(self, message: str = '', **kwargs: Any) -> None:
if self.__verbosity >= 0:
print(f'[{message}]')

def abort(self, message='', code=1, **kwargs):
def abort(self, message: str = '', code: int = 1, **kwargs: Any) -> None:
"""
Terminate the program with the given return code.
"""
Expand All @@ -110,12 +113,12 @@ def abort(self, message='', code=1, **kwargs):

sys.exit(code)

def get_safe_application(self):
def get_safe_application(self) -> SafeApplication:
return SafeApplication(self)


class SafeApplication:
def __init__(self, app):
def __init__(self, app: InvokedApplication | Application) -> None:
self.abort = app.abort
self.display_always = app.display_always
self.display_info = app.display_info
Expand All @@ -127,19 +130,19 @@ def __init__(self, app):
self.display_mini_header = app.display_mini_header


def format_app_command(method, *args, **kwargs):
def format_app_command(method: str, *args: Any, **kwargs: Any) -> str:
procedure = pickle.dumps((method, args, kwargs), 4)

return f"__HATCH__:{''.join('%02x' % i for i in procedure)}"


def get_application(called_by_app):
def get_application(called_by_app: bool) -> InvokedApplication | Application:
return InvokedApplication() if called_by_app else Application()


def send_app_command(method, *args, **kwargs):
def send_app_command(method: str, *args: Any, **kwargs: Any) -> None:
_send_app_command(format_app_command(method, *args, **kwargs))


def _send_app_command(command):
def _send_app_command(command: str) -> None:
print(command)
1 change: 1 addition & 0 deletions docs/meta/authors.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@
- Chris Warrick [:material-twitter:](https://twitter.com/Kwpolska)
- Lumír 'Frenzy' Balhar [:material-email:](mailto:[email protected]) [:material-twitter:](https://twitter.com/lumirbalhar)
- Ofek Lev [:material-web:](https://ofek.dev) [:material-github:](https://github.com/ofek) [:material-twitter:](https://twitter.com/Ofekmeister)
- Olga Matoula [:material-github:](https://github.com/olgarithms) [:material-twitter:](https://twitter.com/olgarithms_)
- Philip Blair [:material-email:](mailto:[email protected])
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ namespace_packages = true
[[tool.mypy.overrides]]
module = [
"*.hatchling.build",
"*.hatchling.ouroboros"
"*.hatchling.ouroboros",
"*.hatchling.bridge.*",
]
disallow_untyped_defs = true
disallow_incomplete_defs = true
Expand Down

0 comments on commit dcdbdf1

Please sign in to comment.