Skip to content

Commit 1250500

Browse files
charlesdong1991jorisvandenbossche
authored andcommitted
DOC: Add ignore-deprecate argument to validate_docstrings.py (pandas-dev#23650)
1 parent 24bce1a commit 1250500

File tree

2 files changed

+49
-14
lines changed

2 files changed

+49
-14
lines changed

scripts/tests/test_validate_docstrings.py

+34-10
Original file line numberDiff line numberDiff line change
@@ -817,6 +817,20 @@ def test_bad_examples(self, capsys, klass, func, msgs):
817817
for msg in msgs:
818818
assert msg in ' '.join(err[1] for err in result['errors'])
819819

820+
def test_validate_all_ignore_deprecated(self, monkeypatch):
821+
monkeypatch.setattr(
822+
validate_docstrings, 'validate_one', lambda func_name: {
823+
'docstring': 'docstring1',
824+
'errors': [('ER01', 'err desc'),
825+
('ER02', 'err desc'),
826+
('ER03', 'err desc')],
827+
'warnings': [],
828+
'examples_errors': '',
829+
'deprecated': True})
830+
result = validate_docstrings.validate_all(prefix=None,
831+
ignore_deprecated=True)
832+
assert len(result) == 0
833+
820834

821835
class TestApiItems(object):
822836
@property
@@ -907,12 +921,14 @@ def test_exit_status_for_validate_one(self, monkeypatch):
907921
exit_status = validate_docstrings.main(func_name='docstring1',
908922
prefix=None,
909923
errors=[],
910-
output_format='default')
924+
output_format='default',
925+
ignore_deprecated=False)
911926
assert exit_status == 0
912927

913928
def test_exit_status_errors_for_validate_all(self, monkeypatch):
914929
monkeypatch.setattr(
915-
validate_docstrings, 'validate_all', lambda prefix: {
930+
validate_docstrings, 'validate_all',
931+
lambda prefix, ignore_deprecated=False: {
916932
'docstring1': {'errors': [('ER01', 'err desc'),
917933
('ER02', 'err desc'),
918934
('ER03', 'err desc')],
@@ -925,25 +941,29 @@ def test_exit_status_errors_for_validate_all(self, monkeypatch):
925941
exit_status = validate_docstrings.main(func_name=None,
926942
prefix=None,
927943
errors=[],
928-
output_format='default')
944+
output_format='default',
945+
ignore_deprecated=False)
929946
assert exit_status == 5
930947

931948
def test_no_exit_status_noerrors_for_validate_all(self, monkeypatch):
932949
monkeypatch.setattr(
933-
validate_docstrings, 'validate_all', lambda prefix: {
950+
validate_docstrings, 'validate_all',
951+
lambda prefix, ignore_deprecated=False: {
934952
'docstring1': {'errors': [],
935953
'warnings': [('WN01', 'warn desc')]},
936954
'docstring2': {'errors': []}})
937955
exit_status = validate_docstrings.main(func_name=None,
938956
prefix=None,
939957
errors=[],
940-
output_format='default')
958+
output_format='default',
959+
ignore_deprecated=False)
941960
assert exit_status == 0
942961

943962
def test_exit_status_for_validate_all_json(self, monkeypatch):
944963
print('EXECUTED')
945964
monkeypatch.setattr(
946-
validate_docstrings, 'validate_all', lambda prefix: {
965+
validate_docstrings, 'validate_all',
966+
lambda prefix, ignore_deprecated=False: {
947967
'docstring1': {'errors': [('ER01', 'err desc'),
948968
('ER02', 'err desc'),
949969
('ER03', 'err desc')]},
@@ -952,12 +972,14 @@ def test_exit_status_for_validate_all_json(self, monkeypatch):
952972
exit_status = validate_docstrings.main(func_name=None,
953973
prefix=None,
954974
errors=[],
955-
output_format='json')
975+
output_format='json',
976+
ignore_deprecated=False)
956977
assert exit_status == 0
957978

958979
def test_errors_param_filters_errors(self, monkeypatch):
959980
monkeypatch.setattr(
960-
validate_docstrings, 'validate_all', lambda prefix: {
981+
validate_docstrings, 'validate_all',
982+
lambda prefix, ignore_deprecated=False: {
961983
'Series.foo': {'errors': [('ER01', 'err desc'),
962984
('ER02', 'err desc'),
963985
('ER03', 'err desc')],
@@ -973,11 +995,13 @@ def test_errors_param_filters_errors(self, monkeypatch):
973995
exit_status = validate_docstrings.main(func_name=None,
974996
prefix=None,
975997
errors=['ER01'],
976-
output_format='default')
998+
output_format='default',
999+
ignore_deprecated=False)
9771000
assert exit_status == 3
9781001

9791002
exit_status = validate_docstrings.main(func_name=None,
9801003
prefix=None,
9811004
errors=['ER03'],
982-
output_format='default')
1005+
output_format='default',
1006+
ignore_deprecated=False)
9831007
assert exit_status == 1

scripts/validate_docstrings.py

+15-4
Original file line numberDiff line numberDiff line change
@@ -716,7 +716,7 @@ def validate_one(func_name):
716716
'examples_errors': examples_errs}
717717

718718

719-
def validate_all(prefix):
719+
def validate_all(prefix, ignore_deprecated=False):
720720
"""
721721
Execute the validation of all docstrings, and return a dict with the
722722
results.
@@ -726,6 +726,8 @@ def validate_all(prefix):
726726
prefix : str or None
727727
If provided, only the docstrings that start with this pattern will be
728728
validated. If None, all docstrings will be validated.
729+
ignore_deprecated: bool, default False
730+
If True, deprecated objects are ignored when validating docstrings.
729731
730732
Returns
731733
-------
@@ -744,6 +746,8 @@ def validate_all(prefix):
744746
if prefix and not func_name.startswith(prefix):
745747
continue
746748
doc_info = validate_one(func_name)
749+
if ignore_deprecated and doc_info['deprecated']:
750+
continue
747751
result[func_name] = doc_info
748752

749753
shared_code_key = doc_info['file'], doc_info['file_line']
@@ -765,13 +769,15 @@ def validate_all(prefix):
765769
if prefix and not func_name.startswith(prefix):
766770
continue
767771
doc_info = validate_one(func_name)
772+
if ignore_deprecated and doc_info['deprecated']:
773+
continue
768774
result[func_name] = doc_info
769775
result[func_name]['in_api'] = False
770776

771777
return result
772778

773779

774-
def main(func_name, prefix, errors, output_format):
780+
def main(func_name, prefix, errors, output_format, ignore_deprecated):
775781
def header(title, width=80, char='#'):
776782
full_line = char * width
777783
side_len = (width - len(title) - 2) // 2
@@ -785,7 +791,7 @@ def header(title, width=80, char='#'):
785791

786792
exit_status = 0
787793
if func_name is None:
788-
result = validate_all(prefix)
794+
result = validate_all(prefix, ignore_deprecated)
789795

790796
if output_format == 'json':
791797
output = json.dumps(result)
@@ -876,8 +882,13 @@ def header(title, width=80, char='#'):
876882
'list of error codes to validate. By default it '
877883
'validates all errors (ignored when validating '
878884
'a single docstring)')
885+
argparser.add_argument('--ignore_deprecated', default=False,
886+
action='store_true', help='if this flag is set, '
887+
'deprecated objects are ignored when validating '
888+
'all docstrings')
879889

880890
args = argparser.parse_args()
881891
sys.exit(main(args.function, args.prefix,
882892
args.errors.split(',') if args.errors else None,
883-
args.format))
893+
args.format,
894+
args.ignore_deprecated))

0 commit comments

Comments
 (0)