mirror of
https://github.com/sprockets/sprockets.mixins.mediatype.git
synced 2025-01-01 11:13:21 +00:00
Rename some type aliases.
I found the aliases of the form `typing.Supports*` in the Standard Library today. Decided to rename `HasSettings` and `DefinesIsoFormat` to match.
This commit is contained in:
parent
776f23fee5
commit
65d6693d62
5 changed files with 24 additions and 31 deletions
|
@ -94,8 +94,8 @@ Convenience Types
|
|||
Contract Types
|
||||
~~~~~~~~~~~~~~
|
||||
|
||||
.. autoclass:: HasSettings
|
||||
.. autoclass:: SupportsIsoFormat
|
||||
:members:
|
||||
|
||||
.. autoclass:: DefinesIsoFormat
|
||||
.. autoclass:: SupportsSettings
|
||||
:members:
|
||||
|
|
17
docs/conf.py
17
docs/conf.py
|
@ -36,21 +36,18 @@ intersphinx_mapping = {
|
|||
# and the prefixed name (e.g., type_info.Deserialized) since both forms
|
||||
# appear in the typing annotations.
|
||||
extensions.append('sphinx.ext.autodoc')
|
||||
_names = {
|
||||
'Deserialized', 'DumpSFunction', 'LoadSFunction', 'MsgPackable',
|
||||
'PackBFunction', 'Serializable', 'SupportsIsoFormat', 'SupportsSettings',
|
||||
'Transcoder', 'UnpackBFunction'
|
||||
}
|
||||
autodoc_type_aliases = {
|
||||
alias: f'sprockets.mixins.mediatype.type_info.{alias}'
|
||||
for alias in {
|
||||
'DefinesIsoFormat', 'Deserialized', 'DumpSFunction', 'HasSettings',
|
||||
'LoadSFunction', 'MsgPackable', 'PackBFunction', 'Serializable',
|
||||
'Transcoder', 'UnpackBFunction'
|
||||
}
|
||||
for alias in _names
|
||||
}
|
||||
autodoc_type_aliases.update({
|
||||
f'type_info.{alias}': f'sprockets.mixins.mediatype.type_info.{alias}'
|
||||
for alias in {
|
||||
'DefinesIsoFormat', 'Deserialized', 'DumpSFunction', 'HasSettings',
|
||||
'LoadSFunction', 'MsgPackable', 'PackBFunction', 'Serializable',
|
||||
'Transcoder', 'UnpackBFunction'
|
||||
}
|
||||
for alias in _names
|
||||
})
|
||||
|
||||
# https://www.sphinx-doc.org/en/master/usage/extensions/extlinks.html
|
||||
|
|
|
@ -154,7 +154,7 @@ class ContentSettings:
|
|||
self._default_content_type = new_value
|
||||
|
||||
|
||||
def install(application: type_info.HasSettings,
|
||||
def install(application: type_info.SupportsSettings,
|
||||
default_content_type: typing.Optional[str],
|
||||
encoding: typing.Optional[str] = None) -> ContentSettings:
|
||||
"""Install the media type management settings and return it"""
|
||||
|
@ -170,20 +170,20 @@ def install(application: type_info.HasSettings,
|
|||
|
||||
@typing.overload
|
||||
def get_settings(
|
||||
application: type_info.HasSettings,
|
||||
application: type_info.SupportsSettings,
|
||||
force_instance: Literal[False] = False
|
||||
) -> typing.Union[ContentSettings, None]:
|
||||
... # pragma: no cover
|
||||
|
||||
|
||||
@typing.overload
|
||||
def get_settings(application: type_info.HasSettings,
|
||||
def get_settings(application: type_info.SupportsSettings,
|
||||
force_instance: Literal[True]) -> ContentSettings:
|
||||
... # pragma: no cover
|
||||
|
||||
|
||||
def get_settings(
|
||||
application: type_info.HasSettings,
|
||||
application: type_info.SupportsSettings,
|
||||
force_instance: bool = False) -> typing.Union[ContentSettings, None]:
|
||||
"""
|
||||
Retrieve the media type settings for a application.
|
||||
|
@ -205,7 +205,7 @@ def get_settings(
|
|||
return install(application, None)
|
||||
|
||||
|
||||
def add_binary_content_type(application: type_info.HasSettings,
|
||||
def add_binary_content_type(application: type_info.SupportsSettings,
|
||||
content_type: str, pack: type_info.PackBFunction,
|
||||
unpack: type_info.UnpackBFunction) -> None:
|
||||
"""
|
||||
|
@ -223,7 +223,7 @@ def add_binary_content_type(application: type_info.HasSettings,
|
|||
handlers.BinaryContentHandler(content_type, pack, unpack))
|
||||
|
||||
|
||||
def add_text_content_type(application: type_info.HasSettings,
|
||||
def add_text_content_type(application: type_info.SupportsSettings,
|
||||
content_type: str, default_encoding: str,
|
||||
dumps: type_info.DumpSFunction,
|
||||
loads: type_info.LoadSFunction) -> None:
|
||||
|
@ -251,7 +251,7 @@ def add_text_content_type(application: type_info.HasSettings,
|
|||
default_encoding))
|
||||
|
||||
|
||||
def add_transcoder(application: type_info.HasSettings,
|
||||
def add_transcoder(application: type_info.SupportsSettings,
|
||||
transcoder: type_info.Transcoder,
|
||||
content_type: typing.Optional[str] = None) -> None:
|
||||
"""
|
||||
|
@ -273,7 +273,7 @@ def add_transcoder(application: type_info.HasSettings,
|
|||
settings[content_type or transcoder.content_type] = transcoder
|
||||
|
||||
|
||||
def set_default_content_type(application: type_info.HasSettings,
|
||||
def set_default_content_type(application: type_info.SupportsSettings,
|
||||
content_type: str,
|
||||
encoding: typing.Optional[str] = None) -> None:
|
||||
"""
|
||||
|
|
|
@ -116,7 +116,7 @@ class JSONTranscoder(handlers.TextContentHandler):
|
|||
if isinstance(obj, uuid.UUID):
|
||||
return str(obj)
|
||||
if hasattr(obj, 'isoformat'):
|
||||
return typing.cast(type_info.DefinesIsoFormat, obj).isoformat()
|
||||
return typing.cast(type_info.SupportsIsoFormat, obj).isoformat()
|
||||
if isinstance(obj, (bytes, bytearray, memoryview)):
|
||||
return base64.b64encode(obj).decode('ASCII')
|
||||
raise TypeError('{!r} is not JSON serializable'.format(obj))
|
||||
|
@ -234,7 +234,7 @@ class MsgPackTranscoder(handlers.BinaryContentHandler):
|
|||
datum = datum.tobytes()
|
||||
|
||||
if hasattr(datum, 'isoformat'):
|
||||
datum = typing.cast(type_info.DefinesIsoFormat, datum).isoformat()
|
||||
datum = typing.cast(type_info.SupportsIsoFormat, datum).isoformat()
|
||||
|
||||
if isinstance(datum, (bytes, str)):
|
||||
return datum
|
||||
|
@ -429,7 +429,7 @@ class FormUrlEncodedTranscoder:
|
|||
return dict(output)
|
||||
|
||||
def _encode(self, datum: typing.Union[bool, None, float, int, str,
|
||||
type_info.DefinesIsoFormat],
|
||||
type_info.SupportsIsoFormat],
|
||||
char_map: typing.Mapping[int, str], encoding: str) -> str:
|
||||
if isinstance(datum, str):
|
||||
pass # optimization: skip additional checks for strings
|
||||
|
@ -442,7 +442,7 @@ class FormUrlEncodedTranscoder:
|
|||
datum = self.options.literal_mapping[datum] # type: ignore
|
||||
elif isinstance(datum, (bytearray, bytes, memoryview)):
|
||||
return ''.join(char_map[c] for c in datum)
|
||||
elif isinstance(datum, type_info.DefinesIsoFormat):
|
||||
elif isinstance(datum, type_info.SupportsIsoFormat):
|
||||
datum = datum.isoformat()
|
||||
else:
|
||||
datum = str(datum)
|
||||
|
|
|
@ -12,24 +12,20 @@ except ImportError:
|
|||
|
||||
|
||||
@runtime_checkable
|
||||
class DefinesIsoFormat(Protocol):
|
||||
class SupportsIsoFormat(Protocol):
|
||||
"""An object that has an isoformat method."""
|
||||
def isoformat(self) -> str:
|
||||
"""Return the date/time in ISO-8601 format."""
|
||||
...
|
||||
|
||||
|
||||
class HasSettings(Protocol):
|
||||
class SupportsSettings(Protocol):
|
||||
"""Something that quacks like a tornado.web.Application."""
|
||||
settings: typing.Dict[str, typing.Any]
|
||||
"""Application settings."""
|
||||
|
||||
|
||||
SerializablePrimitives = (type(None), bool, bytearray, bytes, float, int,
|
||||
memoryview, str, uuid.UUID)
|
||||
"""Use this with isinstance to identify simple values."""
|
||||
|
||||
Serializable = typing.Union[DefinesIsoFormat, None, bool, bytearray, bytes,
|
||||
Serializable = typing.Union[SupportsIsoFormat, None, bool, bytearray, bytes,
|
||||
float, int, memoryview, str, typing.Mapping,
|
||||
typing.Sequence, typing.Set, uuid.UUID]
|
||||
"""Types that can be serialized by this library.
|
||||
|
|
Loading…
Reference in a new issue