-
Notifications
You must be signed in to change notification settings - Fork 203
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
馃摑 Rename Websocket by WebSocket #272
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f6d01f4
:pencil2: Rename Websocket by WebSocket
Kludex ae68a1b
add backwards-compatible fashion using pep562
Kludex c2f864a
add tests to non deprecated types
Kludex c527b6e
use same pattern on both tests
Kludex 0a6acb2
use pep562 as vendor instead of dependency
Kludex 1e720e0
fix with pre-commit
Kludex File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
""" | ||
Backport of PEP 562. | ||
https://pypi.org/search/?q=pep562 | ||
Licensed under MIT | ||
Copyright (c) 2018 Isaac Muse <isaacmuse@gmail.com> | ||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated | ||
documentation files (the "Software"), to deal in the Software without restriction, including without limitation | ||
the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, | ||
and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in all copies or substantial portions | ||
of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED | ||
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | ||
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF | ||
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS | ||
IN THE SOFTWARE. | ||
""" | ||
import sys | ||
from typing import Any, Callable, List, Optional | ||
|
||
|
||
class Pep562: | ||
""" | ||
Backport of PEP 562 <https://pypi.org/search/?q=pep562>. | ||
Wraps the module in a class that exposes the mechanics to override `__dir__` and `__getattr__`. | ||
The given module will be searched for overrides of `__dir__` and `__getattr__` and use them when needed. | ||
""" | ||
|
||
def __init__(self, name: str) -> None: | ||
"""Acquire `__getattr__` and `__dir__`, but only replace module for versions less than Python 3.7.""" | ||
|
||
self._module = sys.modules[name] | ||
self._get_attr = getattr(self._module, "__getattr__", None) | ||
self._get_dir: Optional[Callable[..., List[str]]] = getattr( | ||
self._module, "__dir__", None | ||
) | ||
sys.modules[name] = self # type: ignore[assignment] | ||
|
||
def __dir__(self) -> List[str]: | ||
"""Return the overridden `dir` if one was provided, else apply `dir` to the module.""" | ||
|
||
return self._get_dir() if self._get_dir else dir(self._module) | ||
|
||
def __getattr__(self, name: str) -> Any: | ||
""" | ||
Attempt to retrieve the attribute from the module, and if missing, use the overridden function if present. | ||
""" | ||
|
||
try: | ||
return getattr(self._module, name) | ||
except AttributeError: | ||
if self._get_attr: | ||
return self._get_attr(name) | ||
raise | ||
|
||
|
||
def pep562(module_name: str) -> None: | ||
"""Helper function to apply PEP 562.""" | ||
|
||
if sys.version_info < (3, 7): | ||
Pep562(module_name) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import importlib | ||
|
||
import pytest | ||
|
||
from asgiref import typing | ||
|
||
|
||
@pytest.mark.parametrize("deprecated_type", typing.__deprecated__.keys()) | ||
def test_deprecated_types(deprecated_type: str) -> None: | ||
with pytest.warns(DeprecationWarning) as record: | ||
getattr(importlib.import_module("asgiref.typing"), deprecated_type) | ||
assert len(record) == 1 | ||
assert deprecated_type in str(record.list[0]) | ||
|
||
|
||
@pytest.mark.parametrize("available_type", typing.__all__) | ||
def test_available_types(available_type: str) -> None: | ||
with pytest.warns(None) as record: | ||
getattr(importlib.import_module("asgiref.typing"), available_type) | ||
assert len(record) == 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sys.modules
expects aModule
andself
is aPep562
, jfyk.