Skip to content

Commit

Permalink
Fix linting issues
Browse files Browse the repository at this point in the history
# Summary

This fixes formatting issues identified by the linter.  No functional changes are included.

# Tests

- hatch run lint passes
- hatch run test passes (and coverage is sufficient)
  • Loading branch information
richardt-engineb committed Jan 25, 2024
1 parent 985cfc0 commit bdccca0
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 34 deletions.
6 changes: 3 additions & 3 deletions frictionless/resource/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,11 +273,11 @@ def __enter__(self):
would result in errors because the second context would close the file
before the write happened. While the above code is obvious, similar
things can happen when composing steps in pipelines, calling petl code etc.
things can happen when composing steps in pipelines, calling petl code etc.
where the various functions may have no knowledge of each other.
See #1622 for more details.
So we only allow a single context to be open at a time, and raise an
So we only allow a single context to be open at a time, and raise an
exception if nested context is attempted. For similar reasons, we
also raise an exception if a context is attempted on an open resource.
Expand All @@ -298,7 +298,7 @@ def __enter__(self):
if self.__context_manager_entered:
note = "Resource has previously entered a context manager (`with` statement) and does not support nested contexts. To use in a nested context use `to_copy()` then use the copy in the `with`."
raise FrictionlessException(note)
if self.closed == False:
if not self.closed:
note = "Resource is currently open, and cannot be used in a `with` statement (which would reopen the file). To use `with` on an open Resouece, use to_copy() then use the copy in the `with`."
raise FrictionlessException(note)

Expand Down
4 changes: 3 additions & 1 deletion frictionless/validator/validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,9 @@ def validate_resource(
errors.append(error)

# Validate file
if not isinstance(resource_copy, platform.frictionless_resources.TableResource):
if not isinstance(
resource_copy, platform.frictionless_resources.TableResource
):
if resource_copy.hash is not None or resource_copy.bytes is not None:
helpers.pass_through(resource_copy.byte_stream)

Expand Down
37 changes: 19 additions & 18 deletions tests/resource/test_context_manager.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from frictionless import Resource, FrictionlessException
import pytest

from frictionless import FrictionlessException, Resource

# Test that the context manager implementation works correctly

# As per PEP-343, the context manager should be a single-use object (like files)
Expand Down Expand Up @@ -40,38 +41,38 @@ def test_resource_copy_can_use_nested_context():
with Resource("data/table.csv") as resource:
copy = resource.to_copy()
with copy:
assert (copy.closed is False)
assert (resource.closed is False)
assert copy.closed is False
assert resource.closed is False

# Check that the original resource is still open
assert (copy.closed is True)
assert (resource.closed is False)
assert copy.closed is True
assert resource.closed is False


def test_resource_can_use_repeated_non_nested_contexts():
# Repeat context allowed
resource = Resource("data/table.csv")
with resource:
assert (resource.closed is False)
assert resource.closed is False

assert (resource.closed is True)
assert resource.closed is True

with resource:
assert (resource.closed is False)
assert (resource.closed is True)
assert resource.closed is False
assert resource.closed is True


def test_resource_copy_can_use_repeated_context():
# Repeated context with a copy is allowed
resource = Resource("data/table.csv")
copy = resource.to_copy()
with resource:
assert (resource.closed is False)
assert (copy.closed is True)
assert resource.closed is False
assert copy.closed is True

with copy:
assert (resource.closed is True)
assert (copy.closed is False)
assert resource.closed is True
assert copy.closed is False


def test_context_manager_on_open_resource_throw_exception():
Expand All @@ -81,7 +82,7 @@ def test_context_manager_on_open_resource_throw_exception():
"""
resource = Resource("data/table.csv")
resource.open()
assert (resource.closed is False)
assert resource.closed is False
with pytest.raises(FrictionlessException):
with resource:
pass
Expand All @@ -93,10 +94,10 @@ def test_explicit_open_can_be_repeated():
# using explicit open() calls must be aware of that.
resource = Resource("data/table.csv")
resource.open()
assert (resource.closed is False)
assert resource.closed is False
resource.open()
assert (resource.closed is False)
assert resource.closed is False
resource.close()
assert (resource.closed is True)
assert resource.closed is True
resource.close()
assert (resource.closed is True)
assert resource.closed is True
67 changes: 55 additions & 12 deletions tests/table/test_to_petl.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
from frictionless import Resource, FrictionlessException
from petl import util

from frictionless.resources import TableResource


def __assert_nth_row(it, n, expected):
"""
A helper function to assert that the nth row of an iterator is as expected.
"""
for _ in range(n-1):
for _ in range(n - 1):
next(it)
assert next(it) == expected


def test_to_petl_gives_valid_table():
resource = Resource("data/table.csv")
resource = TableResource("data/table.csv")
table = resource.to_petl()
assert util.header(table) == ("id", "name")


def test_to_petl_is_iterable():
resource = Resource("data/table.csv")
resource = TableResource("data/table.csv")
table = resource.to_petl()
it = iter(table)
assert next(it) == ["id", "name"]
Expand All @@ -27,7 +28,7 @@ def test_to_petl_is_iterable():


def test_to_petl_iterators_are_independent():
resource = Resource("data/table.csv")
resource = TableResource("data/table.csv")
table = resource.to_petl()
it1 = iter(table)
it2 = iter(table)
Expand All @@ -46,7 +47,7 @@ def test_to_petl_iterators_are_independent():


def test_to_petl_iterators_have_independent_lifetime():
resource = Resource("data/table-1MB.csv")
resource = TableResource("data/table-1MB.csv")
table = resource.to_petl()
it1 = iter(table)

Expand All @@ -56,19 +57,61 @@ def test_to_petl_iterators_have_independent_lifetime():
# but the buffer is not, so you would get away with incorrectly closing the
# resource if you remain within the buffer).
# See #1622 for more.
__assert_nth_row(it1, 101, [
"ahltic", "22354", "428.17", "382.54", "false", "1926-09-15T01:15:27Z", "1956-04-14", "08:20:13", "4,5", "{\"x\":1,\"y\":7}"])
__assert_nth_row(
it1,
101,
[
"ahltic",
"22354",
"428.17",
"382.54",
"false",
"1926-09-15T01:15:27Z",
"1956-04-14",
"08:20:13",
"4,5",
'{"x":1,"y":7}',
],
)

# Make a local function to give it2 a different scope
def read_from_it2():
it2 = iter(table)
__assert_nth_row(it2, 101, [
"ahltic", "22354", "428.17", "382.54", "false", "1926-09-15T01:15:27Z", "1956-04-14", "08:20:13", "4,5", "{\"x\":1,\"y\":7}"])
__assert_nth_row(
it2,
101,
[
"ahltic",
"22354",
"428.17",
"382.54",
"false",
"1926-09-15T01:15:27Z",
"1956-04-14",
"08:20:13",
"4,5",
'{"x":1,"y":7}',
],
)

# Read from it2 within the nested function scope
read_from_it2()

# Check we can stil read from it1 from where we left off
# Prior to the fix for #1622 this would throw an exception: "ValueError: I/O operation on closed file."
__assert_nth_row(it1, 101, [
"tlbmv8", "91378", "101.19", "832.96", "false", "1983-02-26T12:44:52Z", "1960-08-28", "04:44:23", "5,6", "{\"x\":9,\"y\":4}"])
__assert_nth_row(
it1,
101,
[
"tlbmv8",
"91378",
"101.19",
"832.96",
"false",
"1983-02-26T12:44:52Z",
"1960-08-28",
"04:44:23",
"5,6",
'{"x":9,"y":4}',
],
)

0 comments on commit bdccca0

Please sign in to comment.