From 8655fdd4a968f1b5df4e99acab9381349f22767f Mon Sep 17 00:00:00 2001 From: thoo Date: Mon, 15 Oct 2018 01:01:47 -0400 Subject: [PATCH 01/10] Check numpy and pandas in Examples --- scripts/tests/test_validate_docstrings.py | 20 +++++++++++++++++++- scripts/validate_docstrings.py | 6 ++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/scripts/tests/test_validate_docstrings.py b/scripts/tests/test_validate_docstrings.py index 27c63e3ba3a79..01d7b0b4bd3bb 100644 --- a/scripts/tests/test_validate_docstrings.py +++ b/scripts/tests/test_validate_docstrings.py @@ -502,6 +502,20 @@ def no_punctuation(self): return "Hello world!" +class BadExamples(object): + + def npPd_import(self): + """ + Provide example with numpy and pandas import + + Examples + -------- + import numpy as np + import pandas as pd + """ + pass + + class TestValidator(object): def _import_path(self, klass=None, func=None): @@ -600,7 +614,11 @@ def test_bad_generic_functions(self, func): pytest.param('BadReturns', 'no_description', ('foo',), marks=pytest.mark.xfail), pytest.param('BadReturns', 'no_punctuation', ('foo',), - marks=pytest.mark.xfail) + marks=pytest.mark.xfail), + # Examples + ('BadExamples', 'npPd_import', + ('Examples should not have `import pandas as pd` ', + 'Examples should not have `import numpy as np` ',)) ]) def test_bad_examples(self, capsys, klass, func, msgs): result = validate_one(self._import_path(klass=klass, func=func)) # noqa:F821 diff --git a/scripts/validate_docstrings.py b/scripts/validate_docstrings.py index 6588522331433..a38bf80cb9739 100755 --- a/scripts/validate_docstrings.py +++ b/scripts/validate_docstrings.py @@ -512,6 +512,12 @@ def validate_one(func_name): examples_errs = doc.examples_errors if examples_errs: errs.append('Examples do not pass tests') + if 'import numpy as np' in ' '.join(doc.examples): + errs.append(' Examples should not have ' + '`import numpy as np` ') + if 'import pandas as pd' in ' '.join(doc.examples): + errs.append(' Examples should not have ' + '`import pandas as pd` ') return {'type': doc.type, 'docstring': doc.clean_doc, From 5c071ec42dcc413826e773ab3f41150905ceead1 Mon Sep 17 00:00:00 2001 From: thoo Date: Tue, 16 Oct 2018 02:33:39 -0400 Subject: [PATCH 02/10] Made changes as recommended --- scripts/tests/test_validate_docstrings.py | 42 ++++++++++++++++++----- scripts/validate_docstrings.py | 17 +++++---- 2 files changed, 45 insertions(+), 14 deletions(-) diff --git a/scripts/tests/test_validate_docstrings.py b/scripts/tests/test_validate_docstrings.py index 01d7b0b4bd3bb..e288b19f5852e 100644 --- a/scripts/tests/test_validate_docstrings.py +++ b/scripts/tests/test_validate_docstrings.py @@ -218,6 +218,18 @@ def mode(self, axis, numeric_only): """ pass + def good_import(self): + """ + Ensure import other than numpy and pandas are fine. + + Examples + -------- + This example does not import pandas or import numpy. + >>> import time + >>> import datetime + """ + pass + class BadGenericDocStrings(object): """Everything here has a bad docstring @@ -504,14 +516,27 @@ def no_punctuation(self): class BadExamples(object): - def npPd_import(self): + def numpy_import(self): + """ + Provide example with numpy import. + + Examples + -------- + This example does not import pandas. + >>> import numpy as np + >>> import datetime + """ + pass + + def pandas_import(self): """ - Provide example with numpy and pandas import + Provide example with pandas import. Examples -------- - import numpy as np - import pandas as pd + This example does not import numpy. + >>> import pandas as pd + >>> import pickle """ pass @@ -615,10 +640,11 @@ def test_bad_generic_functions(self, func): marks=pytest.mark.xfail), pytest.param('BadReturns', 'no_punctuation', ('foo',), marks=pytest.mark.xfail), - # Examples - ('BadExamples', 'npPd_import', - ('Examples should not have `import pandas as pd` ', - 'Examples should not have `import numpy as np` ',)) + # Examples tests + ('BadExamples', 'numpy_import', + ('Examples should not have `import numpy` ',)), + ('BadExamples', 'pandas_import', + ('Examples should not have `import pandas` ',)) ]) def test_bad_examples(self, capsys, klass, func, msgs): result = validate_one(self._import_path(klass=klass, func=func)) # noqa:F821 diff --git a/scripts/validate_docstrings.py b/scripts/validate_docstrings.py index a38bf80cb9739..8797e55a8d673 100755 --- a/scripts/validate_docstrings.py +++ b/scripts/validate_docstrings.py @@ -402,6 +402,12 @@ def examples_errors(self): error_msgs += f.getvalue() return error_msgs + @property + def examples_codes(self): + codes = doctest.DocTestParser().get_examples(self.raw_doc) + codes = [line.source.rstrip() for line in codes] + return ' '.join(codes) + def validate_one(func_name): """ @@ -512,12 +518,11 @@ def validate_one(func_name): examples_errs = doc.examples_errors if examples_errs: errs.append('Examples do not pass tests') - if 'import numpy as np' in ' '.join(doc.examples): - errs.append(' Examples should not have ' - '`import numpy as np` ') - if 'import pandas as pd' in ' '.join(doc.examples): - errs.append(' Examples should not have ' - '`import pandas as pd` ') + examples_codes = doc.examples_codes + if 'import numpy' in examples_codes: + errs.append('Examples should not have `import numpy` ') + if 'import pandas' in examples_codes: + errs.append('Examples should not have `import pandas` ') return {'type': doc.type, 'docstring': doc.clean_doc, From 5aff7271c23e3a2db1e77bffacf4154b06da1729 Mon Sep 17 00:00:00 2001 From: thoo Date: Tue, 16 Oct 2018 03:48:12 -0400 Subject: [PATCH 03/10] remove rstrip --- scripts/validate_docstrings.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/validate_docstrings.py b/scripts/validate_docstrings.py index 8797e55a8d673..aadfd77def7ba 100755 --- a/scripts/validate_docstrings.py +++ b/scripts/validate_docstrings.py @@ -405,7 +405,7 @@ def examples_errors(self): @property def examples_codes(self): codes = doctest.DocTestParser().get_examples(self.raw_doc) - codes = [line.source.rstrip() for line in codes] + codes = [line.source for line in codes] return ' '.join(codes) From 374c9eaba74339d20509795a939843114222a808 Mon Sep 17 00:00:00 2001 From: thoo Date: Sat, 27 Oct 2018 09:48:40 -0400 Subject: [PATCH 04/10] remove BadExamples class --- scripts/tests/test_validate_docstrings.py | 31 ++--------------------- 1 file changed, 2 insertions(+), 29 deletions(-) diff --git a/scripts/tests/test_validate_docstrings.py b/scripts/tests/test_validate_docstrings.py index e288b19f5852e..8633e084fa103 100644 --- a/scripts/tests/test_validate_docstrings.py +++ b/scripts/tests/test_validate_docstrings.py @@ -514,33 +514,6 @@ def no_punctuation(self): return "Hello world!" -class BadExamples(object): - - def numpy_import(self): - """ - Provide example with numpy import. - - Examples - -------- - This example does not import pandas. - >>> import numpy as np - >>> import datetime - """ - pass - - def pandas_import(self): - """ - Provide example with pandas import. - - Examples - -------- - This example does not import numpy. - >>> import pandas as pd - >>> import pickle - """ - pass - - class TestValidator(object): def _import_path(self, klass=None, func=None): @@ -641,9 +614,9 @@ def test_bad_generic_functions(self, func): pytest.param('BadReturns', 'no_punctuation', ('foo',), marks=pytest.mark.xfail), # Examples tests - ('BadExamples', 'numpy_import', + ('BadGenericDocStrings', 'method', ('Examples should not have `import numpy` ',)), - ('BadExamples', 'pandas_import', + ('BadGenericDocStrings', 'method', ('Examples should not have `import pandas` ',)) ]) def test_bad_examples(self, capsys, klass, func, msgs): From 3e512ac34205ecee30126598533fb51fedda6384 Mon Sep 17 00:00:00 2001 From: thoo Date: Sat, 3 Nov 2018 13:45:46 -0400 Subject: [PATCH 05/10] Change as recommended --- scripts/tests/test_validate_docstrings.py | 6 +++--- scripts/validate_docstrings.py | 16 +++++++++------- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/scripts/tests/test_validate_docstrings.py b/scripts/tests/test_validate_docstrings.py index 794ecbd41f177..e6405798d04bc 100644 --- a/scripts/tests/test_validate_docstrings.py +++ b/scripts/tests/test_validate_docstrings.py @@ -218,7 +218,7 @@ def mode(self, axis, numeric_only): """ pass - def good_import(self): + def good_imports(self): """ Ensure import other than numpy and pandas are fine. @@ -714,9 +714,9 @@ def test_bad_generic_functions(self, func): marks=pytest.mark.xfail), # Examples tests ('BadGenericDocStrings', 'method', - ('Examples should not have `import numpy` ',)), + ('Numpy does not need to be imported in the examples,')), ('BadGenericDocStrings', 'method', - ('Examples should not have `import pandas` ',)), + ('Pandas does not need to be imported in the examples,')), # See Also tests ('BadSeeAlso', 'prefix_pandas', ('pandas.Series.rename in `See Also` section ' diff --git a/scripts/validate_docstrings.py b/scripts/validate_docstrings.py index a2de9c6f3c609..97e8dce7dae4b 100755 --- a/scripts/validate_docstrings.py +++ b/scripts/validate_docstrings.py @@ -403,10 +403,10 @@ def examples_errors(self): return error_msgs @property - def examples_codes(self): + def examples_source_code(self): codes = doctest.DocTestParser().get_examples(self.raw_doc) codes = [line.source for line in codes] - return ' '.join(codes) + return codes def validate_one(func_name): @@ -537,11 +537,13 @@ def validate_one(func_name): examples_errs = doc.examples_errors if examples_errs: errs.append('Examples do not pass tests') - examples_codes = doc.examples_codes - if 'import numpy' in examples_codes: - errs.append('Examples should not have `import numpy` ') - if 'import pandas' in examples_codes: - errs.append('Examples should not have `import pandas` ') + examples_source_code = doc.examples_source_code + if 'import numpy' in " ".join(examples_source_code): + errs.append("Numpy does not need to be imported in the examples, " + "as it's assumed to be already imported as np") + if 'import pandas' in " ".join(examples_source_code): + errs.append("Pandas does not need to be imported in the examples, " + "as it's assumed to be already imported as pd") return {'type': doc.type, 'docstring': doc.clean_doc, From a2011b22f7f53fac535e39f2011289bc9377fd69 Mon Sep 17 00:00:00 2001 From: thoo Date: Sat, 3 Nov 2018 13:49:14 -0400 Subject: [PATCH 06/10] Change double to single quote --- scripts/validate_docstrings.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/validate_docstrings.py b/scripts/validate_docstrings.py index 97e8dce7dae4b..dc4b2761fcb70 100755 --- a/scripts/validate_docstrings.py +++ b/scripts/validate_docstrings.py @@ -538,10 +538,10 @@ def validate_one(func_name): if examples_errs: errs.append('Examples do not pass tests') examples_source_code = doc.examples_source_code - if 'import numpy' in " ".join(examples_source_code): + if 'import numpy' in ' '.join(examples_source_code): errs.append("Numpy does not need to be imported in the examples, " "as it's assumed to be already imported as np") - if 'import pandas' in " ".join(examples_source_code): + if 'import pandas' in ' '.join(examples_source_code): errs.append("Pandas does not need to be imported in the examples, " "as it's assumed to be already imported as pd") From 0243b9135904728227b9fac8c429aa7df41f89c0 Mon Sep 17 00:00:00 2001 From: thoo Date: Sat, 3 Nov 2018 14:55:19 -0400 Subject: [PATCH 07/10] a few more changes --- scripts/validate_docstrings.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/scripts/validate_docstrings.py b/scripts/validate_docstrings.py index dc4b2761fcb70..4248b1939c349 100755 --- a/scripts/validate_docstrings.py +++ b/scripts/validate_docstrings.py @@ -405,8 +405,8 @@ def examples_errors(self): @property def examples_source_code(self): codes = doctest.DocTestParser().get_examples(self.raw_doc) - codes = [line.source for line in codes] - return codes + lines = [line.source for line in codes] + return lines def validate_one(func_name): @@ -537,12 +537,12 @@ def validate_one(func_name): examples_errs = doc.examples_errors if examples_errs: errs.append('Examples do not pass tests') - examples_source_code = doc.examples_source_code - if 'import numpy' in ' '.join(examples_source_code): - errs.append("Numpy does not need to be imported in the examples, " + examples_source_code = ''.join(doc.examples_source_code) + if 'import numpy' in examples_source_code: + errs.append("numpy does not need to be imported in the examples, " "as it's assumed to be already imported as np") - if 'import pandas' in ' '.join(examples_source_code): - errs.append("Pandas does not need to be imported in the examples, " + if 'import pandas' in examples_source_code: + errs.append("pandas does not need to be imported in the examples, " "as it's assumed to be already imported as pd") return {'type': doc.type, From d123b5ce648302a581e07521c1e01459888d4e23 Mon Sep 17 00:00:00 2001 From: thoo Date: Sat, 3 Nov 2018 14:57:04 -0400 Subject: [PATCH 08/10] a few more changes --- scripts/tests/test_validate_docstrings.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/tests/test_validate_docstrings.py b/scripts/tests/test_validate_docstrings.py index e6405798d04bc..aa8a1500d9d3d 100644 --- a/scripts/tests/test_validate_docstrings.py +++ b/scripts/tests/test_validate_docstrings.py @@ -714,9 +714,9 @@ def test_bad_generic_functions(self, func): marks=pytest.mark.xfail), # Examples tests ('BadGenericDocStrings', 'method', - ('Numpy does not need to be imported in the examples,')), + ('numpy does not need to be imported in the examples,')), ('BadGenericDocStrings', 'method', - ('Pandas does not need to be imported in the examples,')), + ('pandas does not need to be imported in the examples,')), # See Also tests ('BadSeeAlso', 'prefix_pandas', ('pandas.Series.rename in `See Also` section ' From ec78655ae1befed7b70b1cc6be2f5df901ca0774 Mon Sep 17 00:00:00 2001 From: thoo Date: Sat, 3 Nov 2018 15:08:42 -0400 Subject: [PATCH 09/10] changes in return --- scripts/validate_docstrings.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/scripts/validate_docstrings.py b/scripts/validate_docstrings.py index 4248b1939c349..4c54762f6df31 100755 --- a/scripts/validate_docstrings.py +++ b/scripts/validate_docstrings.py @@ -404,9 +404,8 @@ def examples_errors(self): @property def examples_source_code(self): - codes = doctest.DocTestParser().get_examples(self.raw_doc) - lines = [line.source for line in codes] - return lines + lines = doctest.DocTestParser().get_examples(self.raw_doc) + return [line.source for line in lines] def validate_one(func_name): From ce37c8a7b45992415ec828a70ea0dddd4c6781df Mon Sep 17 00:00:00 2001 From: thoo Date: Sat, 3 Nov 2018 20:45:04 -0400 Subject: [PATCH 10/10] Retrigger Travis CI