diff --git a/httpie/output/formatters/colors.py b/httpie/output/formatters/colors.py index 52f5a22a..cf4c8e26 100644 --- a/httpie/output/formatters/colors.py +++ b/httpie/output/formatters/colors.py @@ -1,5 +1,6 @@ from __future__ import absolute_import import json +from typing import Type import pygments.lexer import pygments.token @@ -13,6 +14,7 @@ from pygments.lexers.text import HttpLexer as PygmentsHttpLexer from pygments.util import ClassNotFound from httpie.compat import is_windows +from httpie.context import Environment from httpie.plugins import FormatterPlugin @@ -40,8 +42,13 @@ class ColorFormatter(FormatterPlugin): """ group_name = 'colors' - def __init__(self, env, explicit_json=False, - color_scheme=DEFAULT_STYLE, **kwargs): + def __init__( + self, + env: Environment, + explicit_json=False, + color_scheme=DEFAULT_STYLE, + **kwargs + ): super().__init__(**kwargs) if not env.colors: @@ -63,14 +70,14 @@ class ColorFormatter(FormatterPlugin): self.formatter = formatter self.http_lexer = http_lexer - def format_headers(self, headers): + def format_headers(self, headers: str) -> str: return pygments.highlight( code=headers, lexer=self.http_lexer, formatter=self.formatter, ).strip() - def format_body(self, body, mime): + def format_body(self, body: str, mime: str) -> str: lexer = self.get_lexer_for_body(mime, body) if lexer: body = pygments.highlight( @@ -80,21 +87,29 @@ class ColorFormatter(FormatterPlugin): ) return body.strip() - def get_lexer_for_body(self, mime, body): + def get_lexer_for_body( + self, mime: str, + body: str + ) -> Type[pygments.lexer.Lexer]: return get_lexer( mime=mime, explicit_json=self.explicit_json, body=body, ) - def get_style_class(self, color_scheme): + @staticmethod + def get_style_class(color_scheme: str) -> Type[pygments.style.Style]: try: return pygments.styles.get_style_by_name(color_scheme) except ClassNotFound: return Solarized256Style -def get_lexer(mime, explicit_json=False, body=''): +def get_lexer( + mime: str, + explicit_json=False, + body='' +) -> Type[pygments.lexer.Lexer]: # Build candidate mime type and lexer names. mime_types, lexer_names = [mime], [] diff --git a/httpie/output/formatters/headers.py b/httpie/output/formatters/headers.py index 146dea3c..486f09f6 100644 --- a/httpie/output/formatters/headers.py +++ b/httpie/output/formatters/headers.py @@ -3,7 +3,7 @@ from httpie.plugins import FormatterPlugin class HeadersFormatter(FormatterPlugin): - def format_headers(self, headers): + def format_headers(self, headers: str) -> str: """ Sorts headers by name while retaining relative order of multiple headers with the same name. diff --git a/httpie/output/formatters/json.py b/httpie/output/formatters/json.py index 9af5db1a..d2eb52d3 100644 --- a/httpie/output/formatters/json.py +++ b/httpie/output/formatters/json.py @@ -9,7 +9,7 @@ DEFAULT_INDENT = 4 class JSONFormatter(FormatterPlugin): - def format_body(self, body, mime): + def format_body(self, body: str, mime: str) -> str: maybe_json = [ 'json', 'javascript', diff --git a/httpie/output/processing.py b/httpie/output/processing.py index 98270985..9bee2621 100644 --- a/httpie/output/processing.py +++ b/httpie/output/processing.py @@ -40,12 +40,12 @@ class Formatting: if p.enabled: self.enabled_plugins.append(p) - def format_headers(self, headers): + def format_headers(self, headers: str) -> str: for p in self.enabled_plugins: headers = p.format_headers(headers) return headers - def format_body(self, content, mime): + def format_body(self, content: str, mime: str) -> str: if is_valid_mime(mime): for p in self.enabled_plugins: content = p.format_body(content, mime)