Replace io.open with builtin open (#11807)

From Python 3 `io.open` is an alias for the builtin `open()` function.

Docs: https://docs.python.org/3/library/io.html#io.open
This commit is contained in:
Kaxil Naik 2020-10-24 05:06:33 +01:00 коммит произвёл GitHub
Родитель 56d72e3ff8
Коммит 7b89a04522
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
6 изменённых файлов: 9 добавлений и 19 удалений

Просмотреть файл

@ -15,7 +15,6 @@
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
import io
import logging
import os
import re
@ -83,7 +82,7 @@ def open_maybe_zipped(fileloc, mode='r'):
if archive and zipfile.is_zipfile(archive):
return zipfile.ZipFile(archive, mode=mode).open(filename)
else:
return io.open(fileloc, mode=mode)
return open(fileloc, mode=mode)
def find_path_from_directory(

Просмотреть файл

@ -26,7 +26,6 @@
"""Setup.py for the {{ PACKAGE_PIP_NAME }} package."""
import io
import logging
import os
import sys
@ -41,9 +40,7 @@ version = '{{ RELEASE_NO_LEADING_ZEROS }}{{VERSION_SUFFIX }}'
my_dir = dirname(__file__)
try:
with io.open(os.path.join(my_dir,
'{{ PROVIDER_PATH }}/{{ README_FILE }}'),
encoding='utf-8') as f:
with open(os.path.join(my_dir, '{{ PROVIDER_PATH }}/{{ README_FILE }}'), encoding='utf-8') as f:
long_description = f.read()
except FileNotFoundError:
long_description = ''

Просмотреть файл

@ -17,7 +17,6 @@
# under the License.
"""Setup.py for the Airflow project."""
import io
import logging
import os
import subprocess
@ -43,7 +42,7 @@ PY3 = sys.version_info[0] == 3
my_dir = dirname(__file__)
try:
with io.open(os.path.join(my_dir, 'README.md'), encoding='utf-8') as f:
with open(os.path.join(my_dir, 'README.md'), encoding='utf-8') as f:
long_description = f.read()
except FileNotFoundError:
long_description = ''

Просмотреть файл

@ -14,7 +14,6 @@
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
import io
import json
import os
import sys
@ -152,7 +151,7 @@ def get_imports_from_file(file_name: str) -> List[str]:
:return: list of import names
"""
try:
with io.open(file_name, "rt", encoding="utf-8") as f:
with open(file_name, "rt", encoding="utf-8") as f:
root = parse(f.read(), file_name)
except Exception:
print(f"Error when opening file {file_name}", file=sys.stderr)
@ -245,7 +244,7 @@ if __name__ == '__main__':
print(f"Written provider dependencies to the file {provider_dependencies_file_name}")
print()
if documentation_file_name:
with io.open(documentation_file_name, "r", encoding="utf-8") as documentation_file:
with open(documentation_file_name, "r", encoding="utf-8") as documentation_file:
text = documentation_file.readlines()
replacing = False
result: List[str] = []
@ -258,7 +257,7 @@ if __name__ == '__main__':
replacing = False
if not replacing:
result.append(line)
with io.open(documentation_file_name, "w", encoding="utf-8") as documentation_file:
with open(documentation_file_name, "w", encoding="utf-8") as documentation_file:
documentation_file.write("".join(result))
print()
print(f"Written package extras to the file {documentation_file_name}")

Просмотреть файл

@ -542,17 +542,13 @@ class TestCorrectMaybeZipped(unittest.TestCase):
class TestOpenMaybeZipped(unittest.TestCase):
def test_open_maybe_zipped_normal_file(self):
with mock.patch(
'io.open', mock.mock_open(read_data="data")
) as mock_file:
with mock.patch('builtins.open', mock.mock_open(read_data="data")) as mock_file:
open_maybe_zipped('/path/to/some/file.txt')
mock_file.assert_called_once_with('/path/to/some/file.txt', mode='r')
def test_open_maybe_zipped_normal_file_with_zip_in_name(self):
path = '/path/to/fakearchive.zip.other/file.txt'
with mock.patch(
'io.open', mock.mock_open(read_data="data")
) as mock_file:
with mock.patch('builtins.open', mock.mock_open(read_data="data")) as mock_file:
open_maybe_zipped(path)
mock_file.assert_called_once_with(path, mode='r')

Просмотреть файл

@ -818,7 +818,7 @@ class TestAirflowBaseViews(TestBase):
url = 'code?dag_id=example_bash_operator'
mock_open_patch = mock.mock_open(read_data='')
mock_open_patch.side_effect = FileNotFoundError
with mock.patch('io.open', mock_open_patch):
with mock.patch('builtins.open', mock_open_patch):
resp = self.client.get(url, follow_redirects=True)
self.check_content_in_response('Failed to load file', resp)
self.check_content_in_response('example_bash_operator', resp)