Repository: pandas-dev/pandas
File: pandas/tests/io/test_common.py (Python)

Code before:
    assert icom.is_fsspec_url("gs://pandas/somethingelse.com")
    # the following is the only remote URL that is handled without fsspec
    assert not icom.is_fsspec_url("http://pandas/somethingelse.com")
    assert not icom.is_fsspec_url("random:pandas/somethingelse.com")
    assert not icom.is_fsspec_url("/local/path")
    assert not icom.is_fsspec_url("relative/local/path")
    # fsspec URL in string should not be recognized
    assert not icom.is_fsspec_url("this is not fsspec://url")
    assert not icom.is_fsspec_url("{'url': 'gs://pandas/somethingelse.com'}")
    # accept everything that conforms to RFC 3986 schema
    assert icom.is_fsspec_url("RFC-3986+compliant.spec://something")


def test_is_fsspec_url_chained():
    # GH#48978 Support chained fsspec URLs
    # See https://filesystem-spec.readthedocs.io/en/latest/features.html#url-chaining.
    assert icom.is_fsspec_url("filecache::s3://pandas/test.csv")
    assert icom.is_fsspec_url("zip://test.csv::filecache::gcs://bucket/file.zip")
    assert icom.is_fsspec_url("filecache::zip://test.csv::gcs://bucket/file.zip")
    assert icom.is_fsspec_url("filecache::dask::s3://pandas/test.csv")
    assert not icom.is_fsspec_url("filecache:s3://pandas/test.csv")
    assert not icom.is_fsspec_url("filecache:::s3://pandas/test.csv")
    assert not icom.is_fsspec_url("filecache::://pandas/test.csv")


@pytest.mark.parametrize("encoding", [None, "utf-8"])
@pytest.mark.parametrize("format", ["csv", "json"])
def test_codecs_encoding(encoding, format):
    # GH39247
    expected = pd.DataFrame(
        1.1 * np.arange(120).reshape((30, 4)),
        columns=pd.Index(list("ABCD")),
        index=pd.Index([f"i-{i}" for i in range(30)]),
    )
    with tm.ensure_clean() as path:
        with codecs.open(path, mode="w", encoding=encoding) as handle:
            getattr(expected, f"to_{format}")(handle)
        with codecs.open(path, mode="r", encoding=encoding) as handle:
            if format == "csv":
                df = pd.read_csv(handle, index_col=0)
            else:
                df = pd.read_json(handle)
    tm.assert_frame_equal(expected, df)


def test_codecs_get_writer_reader():
    # GH39247
    expected = pd.DataFrame(
        1.1 * np.arange(120).reshape((30, 4)),
        columns=pd.Index(list("ABCD")),
        index=pd.Index([f"i-{i}" for i in range(30)]),

Review comment (suggestion):
I could also keep testing this but CPython warns about using `open(..., encoding=None)`: `EncodingWarning: 'encoding' argument not specified`. In this case we *are* explicitly specifying `None` but I don't think CPython can tell the difference.


Code after:
    assert icom.is_fsspec_url("gs://pandas/somethingelse.com")
    # the following is the only remote URL that is handled without fsspec
    assert not icom.is_fsspec_url("http://pandas/somethingelse.com")
    assert not icom.is_fsspec_url("random:pandas/somethingelse.com")
    assert not icom.is_fsspec_url("/local/path")
    assert not icom.is_fsspec_url("relative/local/path")
    # fsspec URL in string should not be recognized
    assert not icom.is_fsspec_url("this is not fsspec://url")
    assert not icom.is_fsspec_url("{'url': 'gs://pandas/somethingelse.com'}")
    # accept everything that conforms to RFC 3986 schema
    assert icom.is_fsspec_url("RFC-3986+compliant.spec://something")


def test_is_fsspec_url_chained():
    # GH#48978 Support chained fsspec URLs
    # See https://filesystem-spec.readthedocs.io/en/latest/features.html#url-chaining.
    assert icom.is_fsspec_url("filecache::s3://pandas/test.csv")
    assert icom.is_fsspec_url("zip://test.csv::filecache::gcs://bucket/file.zip")
    assert icom.is_fsspec_url("filecache::zip://test.csv::gcs://bucket/file.zip")
    assert icom.is_fsspec_url("filecache::dask::s3://pandas/test.csv")
    assert not icom.is_fsspec_url("filecache:s3://pandas/test.csv")
    assert not icom.is_fsspec_url("filecache:::s3://pandas/test.csv")
    assert not icom.is_fsspec_url("filecache::://pandas/test.csv")


@pytest.mark.parametrize("format", ["csv", "json"])
def test_codecs_encoding(format):
    # GH39247
    expected = pd.DataFrame(
        1.1 * np.arange(120).reshape((30, 4)),
        columns=pd.Index(list("ABCD")),
        index=pd.Index([f"i-{i}" for i in range(30)]),
    )
    with tm.ensure_clean() as path:
        with open(path, mode="w", encoding="utf-8") as handle:
            getattr(expected, f"to_{format}")(handle)
        with open(path, encoding="utf-8") as handle:
            if format == "csv":
                df = pd.read_csv(handle, index_col=0)
            else:
                df = pd.read_json(handle)
    tm.assert_frame_equal(expected, df)


def test_codecs_get_writer_reader():
    # GH39247
    expected = pd.DataFrame(
        1.1 * np.arange(120).reshape((30, 4)),
        columns=pd.Index(list("ABCD")),
        index=pd.Index([f"i-{i}" for i in range(30)]),
    )