Skip to content
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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MNT: io.ascii deprecate and rename / remove arguments #14914

Merged
merged 13 commits into from Oct 26, 2023
49 changes: 23 additions & 26 deletions astropy/io/ascii/core.py
Expand Up @@ -1637,9 +1637,6 @@ def process_line(self, line):


extra_reader_pars = (
"Reader",
"Inputter",
"Outputter",
"delimiter",
"comment",
"quotechar",
Expand All @@ -1648,8 +1645,8 @@ def process_line(self, line):
"data_end",
"converters",
"encoding",
"data_Splitter",
"header_Splitter",
"data_splitter_cls",
"header_splitter_cls",
"names",
"include_names",
"exclude_names",
Expand All @@ -1660,17 +1657,17 @@ def process_line(self, line):
)


def _get_reader(Reader, Inputter=None, Outputter=None, **kwargs):
def _get_reader(reader_cls, inputter_cls=None, outputter_cls=None, **kwargs):
"""Initialize a table reader allowing for common customizations. See ui.get_reader()
for param docs. This routine is for internal (package) use only and is useful
because it depends only on the "core" module.
"""
from .fastbasic import FastBasic

if issubclass(Reader, FastBasic): # Fast readers handle args separately
if Inputter is not None:
kwargs["Inputter"] = Inputter
return Reader(**kwargs)
if issubclass(reader_cls, FastBasic): # Fast readers handle args separately
if inputter_cls is not None:
kwargs["inputter_cls"] = inputter_cls
return reader_cls(**kwargs)

# If user explicitly passed a fast reader with enable='force'
# (e.g. by passing non-default options), raise an error for slow readers
Expand All @@ -1679,20 +1676,20 @@ def _get_reader(Reader, Inputter=None, Outputter=None, **kwargs):
raise ParameterError(
"fast_reader required with "
"{}, but this is not a fast C reader: {}".format(
kwargs["fast_reader"], Reader
kwargs["fast_reader"], reader_cls
)
)
else:
del kwargs["fast_reader"] # Otherwise ignore fast_reader parameter

reader_kwargs = {k: v for k, v in kwargs.items() if k not in extra_reader_pars}
reader = Reader(**reader_kwargs)
reader = reader_cls(**reader_kwargs)

if Inputter is not None:
reader.inputter = Inputter()
if inputter_cls is not None:
reader.inputter = inputter_cls()

if Outputter is not None:
reader.outputter = Outputter()
if outputter_cls is not None:
reader.outputter = outputter_cls()

# Issue #855 suggested to set data_start to header_start + default_header_length
# Thus, we need to retrieve this from the class definition before resetting these numbers.
Expand Down Expand Up @@ -1739,10 +1736,10 @@ def _get_reader(Reader, Inputter=None, Outputter=None, **kwargs):
raise ValueError("header_start cannot be modified for this Reader")
if "converters" in kwargs:
reader.outputter.converters = kwargs["converters"]
if "data_Splitter" in kwargs:
reader.data.splitter = kwargs["data_Splitter"]()
if "header_Splitter" in kwargs:
reader.header.splitter = kwargs["header_Splitter"]()
if "data_splitter_cls" in kwargs:
reader.data.splitter = kwargs["data_splitter_cls"]()
if "header_splitter_cls" in kwargs:
reader.header.splitter = kwargs["header_splitter_cls"]()
if "names" in kwargs:
reader.names = kwargs["names"]
if None in reader.names:
Expand Down Expand Up @@ -1787,7 +1784,7 @@ def _get_reader(Reader, Inputter=None, Outputter=None, **kwargs):
)


def _get_writer(Writer, fast_writer, **kwargs):
def _get_writer(writer_cls, fast_writer, **kwargs):
"""Initialize a table writer allowing for common customizations. This
routine is for internal (package) use only and is useful because it depends
only on the "core" module.
Expand All @@ -1801,15 +1798,15 @@ def _get_writer(Writer, fast_writer, **kwargs):
if "fill_values" in kwargs and kwargs["fill_values"] is None:
del kwargs["fill_values"]

if issubclass(Writer, FastBasic): # Fast writers handle args separately
return Writer(**kwargs)
elif fast_writer and f"fast_{Writer._format_name}" in FAST_CLASSES:
if issubclass(writer_cls, FastBasic): # Fast writers handle args separately
return writer_cls(**kwargs)
elif fast_writer and f"fast_{writer_cls._format_name}" in FAST_CLASSES:
# Switch to fast writer
kwargs["fast_writer"] = fast_writer
return FAST_CLASSES[f"fast_{Writer._format_name}"](**kwargs)
return FAST_CLASSES[f"fast_{writer_cls._format_name}"](**kwargs)

writer_kwargs = {k: v for k, v in kwargs.items() if k not in extra_writer_pars}
writer = Writer(**writer_kwargs)
writer = writer_cls(**writer_kwargs)

if "delimiter" in kwargs:
writer.header.splitter.delimiter = kwargs["delimiter"]
Expand Down
20 changes: 10 additions & 10 deletions astropy/io/ascii/docs.py
Expand Up @@ -17,9 +17,9 @@
Try to guess the table format. Defaults to None.
format : str, `~astropy.io.ascii.BaseReader`
Input table format
Inputter : `~astropy.io.ascii.BaseInputter`
inputter_cls : `~astropy.io.ascii.BaseInputter`
Inputter class
Outputter : `~astropy.io.ascii.BaseOutputter`
outputter_cls : `~astropy.io.ascii.BaseOutputter`
Outputter class
delimiter : str
Column delimiter string
Expand All @@ -43,9 +43,9 @@
``np.float32``; a list of such types which is tried in order until a
successful conversion is achieved; or a list of converter tuples (see
the `~astropy.io.ascii.convert_numpy` function for details).
data_Splitter : `~astropy.io.ascii.BaseSplitter`
data_splitter_cls : `~astropy.io.ascii.BaseSplitter`
Splitter class to split data columns
header_Splitter : `~astropy.io.ascii.BaseSplitter`
header_splitter_cls : `~astropy.io.ascii.BaseSplitter`
Splitter class to split header columns
names : list
List of names corresponding to each data column
Expand Down Expand Up @@ -89,7 +89,7 @@

"""

# Specify allowed types for core write() keyword arguments. Each entry
# Specify allowed types for core read() keyword arguments. Each entry
# corresponds to the name of an argument and either a type (e.g. int) or a
# list of types. These get used in io.ascii.ui._validate_read_write_kwargs().
# - The commented-out kwargs are too flexible for a useful check
Expand All @@ -98,18 +98,18 @@
# 'table'
"guess": bool,
# 'format'
# 'Reader'
# 'Inputter'
# 'Outputter'
# 'reader_cls'
# 'inputter_cls'
# 'outputter_cls'
"delimiter": str,
"comment": str,
"quotechar": str,
"header_start": int,
"data_start": (int, str), # CDS allows 'guess'
"data_end": int,
"converters": dict,
# 'data_Splitter'
# 'header_Splitter'
# 'data_splitter_cls'
# 'header_splitter_cls'
"names": "list-like",
"include_names": "list-like",
"exclude_names": "list-like",
Expand Down
10 changes: 5 additions & 5 deletions astropy/io/ascii/fastbasic.py
Expand Up @@ -104,15 +104,15 @@ def read(self, table):
raise core.ParameterError(
"The C reader does not use the encoding parameter"
)
elif "Outputter" in self.kwargs:
elif "outputter_cls" in self.kwargs:
raise core.ParameterError(
"The C reader does not use the Outputter parameter"
"The C reader does not use the outputter_cls parameter"
)
elif "Inputter" in self.kwargs:
elif "inputter_cls" in self.kwargs:
raise core.ParameterError(
"The C reader does not use the Inputter parameter"
"The C reader does not use the inputter_cls parameter"
)
elif "data_Splitter" in self.kwargs or "header_Splitter" in self.kwargs:
elif "data_splitter_cls" in self.kwargs or "header_splitter_cls" in self.kwargs:
raise core.ParameterError("The C reader does not use a Splitter class")

self.strict_names = self.kwargs.pop("strict_names", False)
Expand Down
14 changes: 7 additions & 7 deletions astropy/io/ascii/latex.py
Expand Up @@ -244,8 +244,8 @@ class Latex(core.BaseReader):
The default is ``\\begin{table}``. The following would generate a table,
which spans the whole page in a two-column document::

ascii.write(data, sys.stdout, Writer = ascii.Latex,
latexdict = {'tabletype': 'table*'})
ascii.write(data, sys.stdout, format="latex",
latexdict={'tabletype': 'table*'})

If ``None``, the table environment will be dropped, keeping only
the ``tabular`` environment.
Expand Down Expand Up @@ -276,8 +276,8 @@ class Latex(core.BaseReader):

from astropy.io import ascii
data = {'name': ['bike', 'car'], 'mass': [75,1200], 'speed': [10, 130]}
ascii.write(data, Writer=ascii.Latex,
latexdict = {'units': {'mass': 'kg', 'speed': 'km/h'}})
ascii.write(data, format="latex",
latexdict={'units': {'mass': 'kg', 'speed': 'km/h'}})

If the column has no entry in the ``units`` dictionary, it defaults
to the **unit** attribute of the column. If this attribute is not
Expand All @@ -288,18 +288,18 @@ class Latex(core.BaseReader):

from astropy.io import ascii
data = {'cola': [1,2], 'colb': [3,4]}
ascii.write(data, Writer=ascii.Latex, latexdict=ascii.latex.latexdicts['template'])
ascii.write(data, format="latex", latexdict=ascii.latex.latexdicts['template'])

Some table styles are predefined in the dictionary
``ascii.latex.latexdicts``. The following generates in table in
style preferred by A&A and some other journals::

ascii.write(data, Writer=ascii.Latex, latexdict=ascii.latex.latexdicts['AA'])
ascii.write(data, format="latex", latexdict=ascii.latex.latexdicts['AA'])

As an example, this generates a table, which spans all columns
and is centered on the page::

ascii.write(data, Writer=ascii.Latex, col_align='|lr|',
ascii.write(data, format="latex", col_align='|lr|',
latexdict={'preamble': r'\begin{center}',
'tablefoot': r'\end{center}',
'tabletype': 'table*'})
Expand Down
28 changes: 15 additions & 13 deletions astropy/io/ascii/tests/test_c_reader.py
Expand Up @@ -72,7 +72,7 @@ def assert_table_equal(t1, t2, check_meta=False, rtol=1.0e-15, atol=1.0e-300):
def _read(
tmp_path,
table,
Reader=None,
reader_cls=None,
format=None,
parallel=False,
check_meta=False,
Expand All @@ -82,7 +82,7 @@ def _read(
global _filename_counter

table += "\n"
reader = Reader(**kwargs)
reader = reader_cls(**kwargs)
t1 = reader.read(table)
t2 = reader.read(StringIO(table))
t3 = reader.read(table.splitlines())
Expand Down Expand Up @@ -126,34 +126,36 @@ def _read(

@pytest.fixture(scope="function")
def read_basic(tmp_path, request):
return functools.partial(_read, tmp_path, Reader=FastBasic, format="basic")
return functools.partial(_read, tmp_path, reader_cls=FastBasic, format="basic")


@pytest.fixture(scope="function")
def read_csv(tmp_path, request):
return functools.partial(_read, tmp_path, Reader=FastCsv, format="csv")
return functools.partial(_read, tmp_path, reader_cls=FastCsv, format="csv")


@pytest.fixture(scope="function")
def read_tab(tmp_path, request):
return functools.partial(_read, tmp_path, Reader=FastTab, format="tab")
return functools.partial(_read, tmp_path, reader_cls=FastTab, format="tab")


@pytest.fixture(scope="function")
def read_commented_header(tmp_path, request):
return functools.partial(
_read, tmp_path, Reader=FastCommentedHeader, format="commented_header"
_read, tmp_path, reader_cls=FastCommentedHeader, format="commented_header"
)


@pytest.fixture(scope="function")
def read_rdb(tmp_path, request):
return functools.partial(_read, tmp_path, Reader=FastRdb, format="rdb")
return functools.partial(_read, tmp_path, reader_cls=FastRdb, format="rdb")


@pytest.fixture(scope="function")
def read_no_header(tmp_path, request):
return functools.partial(_read, tmp_path, Reader=FastNoHeader, format="no_header")
return functools.partial(
_read, tmp_path, reader_cls=FastNoHeader, format="no_header"
)


@pytest.mark.parametrize("delimiter", [",", "\t", " ", "csv"])
Expand Down Expand Up @@ -517,9 +519,9 @@ def test_quoted_fields(parallel, read_basic):
"converters",
{i + 1: ascii.convert_numpy(np.uint) for i in range(3)},
), # passing converters
("Inputter", ascii.ContinuationLinesInputter), # passing Inputter
("header_Splitter", ascii.DefaultSplitter), # passing Splitter
("data_Splitter", ascii.DefaultSplitter),
("inputter_cls", ascii.ContinuationLinesInputter), # passing inputter_cls
("header_splitter_cls", ascii.DefaultSplitter), # passing Splitter
("data_splitter_cls", ascii.DefaultSplitter),
],
)
def test_invalid_parameters(key, val):
Expand All @@ -538,8 +540,8 @@ def test_invalid_parameters_other():
with pytest.raises(FastOptionsError): # don't fall back on the slow reader
ascii.read("1 2 3\n4 5 6", format="basic", fast_reader={"foo": 7})
with pytest.raises(ParameterError):
# Outputter cannot be specified in constructor
FastBasic(Outputter=ascii.TableOutputter).read("1 2 3\n4 5 6")
# outputter_cls cannot be specified in constructor
FastBasic(outputter_cls=ascii.TableOutputter).read("1 2 3\n4 5 6")


def test_too_many_cols1():
Expand Down
8 changes: 4 additions & 4 deletions astropy/io/ascii/tests/test_cds_header_from_readme.py
Expand Up @@ -20,7 +20,7 @@ def read_table1(readme, data):


def read_table2(readme, data):
reader = ascii.get_reader(Reader=ascii.Cds, readme=readme)
reader = ascii.get_reader(reader_cls=ascii.Cds, readme=readme)
reader.outputter = ascii.TableOutputter()
return reader.read(data)

Expand Down Expand Up @@ -205,7 +205,7 @@ def test_cds_function_units2(reader_cls):


def test_cds_ignore_nullable():
# Make sure CDS Reader does not ignore nullabilty for columns
# Make sure CDS reader_cls does not ignore nullabilty for columns
# with a limit specifier
readme = "data/cds/null/ReadMe"
data = "data/cds/null/table.dat"
Expand All @@ -217,7 +217,7 @@ def test_cds_ignore_nullable():


def test_cds_no_whitespace():
# Make sure CDS Reader only checks null values when an '=' symbol is present,
# Make sure CDS reader_cls only checks null values when an '=' symbol is present,
# and read description text even if there is no whitespace after '?'.
readme = "data/cds/null/ReadMe1"
data = "data/cds/null/table.dat"
Expand All @@ -235,7 +235,7 @@ def test_cds_no_whitespace():


def test_cds_order():
# Make sure CDS Reader does not ignore order specifier that maybe present after
# Make sure CDS reader_cls does not ignore order specifier that maybe present after
# the null specifier '?'
readme = "data/cds/null/ReadMe1"
data = "data/cds/null/table.dat"
Expand Down