Skip to content

Commit 5714d1c

Browse files
neuralsorcerermeta-codesync[bot]
authored andcommitted
Fix CLI file argument deprecations (#134)
Summary: Replaced deprecated argparse `FileType` usage in the CLI with `pathlib.Path` arguments and relied on pandas to handle file opening, eliminating the need for manual handle cleanup and resolving the [`PendingDeprecationWarning`](https://github.com/facebookresearch/balance/actions/runs/19194480616/job/54873409079#step:5:32) noise. Pull Request resolved: #134 Differential Revision: D86629212 Pulled By: talgalili fbshipit-source-id: d59b537ec144c03b7eedef14cb4ccb7d6fae6a65
1 parent 811c269 commit 5714d1c

File tree

1 file changed

+5
-13
lines changed

1 file changed

+5
-13
lines changed

balance/cli.py

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
import inspect
1111
import logging
1212

13-
from argparse import ArgumentParser, FileType, Namespace
13+
from argparse import ArgumentParser, Namespace
14+
from pathlib import Path
1415

1516
from typing import Dict, List, Optional, Tuple, Type, Union
1617

@@ -329,7 +330,6 @@ def write_outputs(self, output_df, diagnostics_df) -> None:
329330
header=(not self.args.no_output_header),
330331
sep=self.args.sep_output_file,
331332
)
332-
self.args.output_file.close()
333333

334334
if self.args.diagnostics_output_file is not None:
335335
diagnostics_df.to_csv(
@@ -338,7 +338,6 @@ def write_outputs(self, output_df, diagnostics_df) -> None:
338338
header=(not self.args.no_output_header),
339339
sep=self.args.sep_diagnostics_output_file,
340340
)
341-
self.args.diagnostics_output_file.close()
342341

343342
def update_attributes_for_main_used_by_adjust(self) -> None:
344343
"""
@@ -515,13 +514,6 @@ def main(self) -> None:
515514
# Write output
516515
self.write_outputs(output_df, diagnostics_df)
517516

518-
def __del__(self) -> None:
519-
for handle in [self.args.input_file, self.args.output_file]:
520-
try:
521-
handle.close()
522-
except FileNotFoundError:
523-
pass
524-
525517

526518
def _float_or_none(value: Union[float, int, str, None]) -> Optional[float]:
527519
"""Return a float (if float or int) or None if it's None or "None"
@@ -544,19 +536,19 @@ def add_arguments_to_parser(parser: ArgumentParser) -> ArgumentParser:
544536
# TODO: add arguments for formula when used as a list and for penalty_factor
545537
parser.add_argument(
546538
"--input_file",
547-
type=FileType("r"),
539+
type=Path,
548540
required=True,
549541
help="Path to input sample/target",
550542
)
551543
parser.add_argument(
552544
"--output_file",
553-
type=FileType("w"),
545+
type=Path,
554546
required=True,
555547
help="Path to write output weights",
556548
)
557549
parser.add_argument(
558550
"--diagnostics_output_file",
559-
type=FileType("w"),
551+
type=Path,
560552
required=False,
561553
help="Path to write adjustment diagnostics",
562554
)

0 commit comments

Comments
 (0)