Skip to content

Commit

Permalink
Replace io.open with builtin open (apache#11807)
Browse files Browse the repository at this point in the history

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

Docs: https://docs.python.org/3/library/io.html#io.open
  • Loading branch information
kaxil authored Oct 24, 2020
1 parent 56d72e3 commit 7b89a04
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 19 deletions.
3 changes: 1 addition & 2 deletions airflow/utils/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down
5 changes: 1 addition & 4 deletions provider_packages/SETUP_TEMPLATE.py.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@

"""Setup.py for the {{ PACKAGE_PIP_NAME }} package."""

import io
import logging
import os
import sys
Expand All @@ -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 = ''
Expand Down
3 changes: 1 addition & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
# under the License.
"""Setup.py for the Airflow project."""

import io
import logging
import os
import subprocess
Expand All @@ -43,7 +42,7 @@
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 = ''
Expand Down
7 changes: 3 additions & 4 deletions tests/build_provider_packages_dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -245,7 +244,7 @@ def insert_documentation(deps_dict: Dict[str, List[str]], res: List[str]):
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] = []
Expand All @@ -258,7 +257,7 @@ def insert_documentation(deps_dict: Dict[str, List[str]], res: List[str]):
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}")
Expand Down
8 changes: 2 additions & 6 deletions tests/utils/test_dag_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -542,17 +542,13 @@ def test_correct_maybe_zipped_archive(self, mocked_is_zipfile):

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')

Expand Down
2 changes: 1 addition & 1 deletion tests/www/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -818,7 +818,7 @@ def test_code_no_file(self):
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)
Expand Down

0 comments on commit 7b89a04

Please sign in to comment.