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

attempt for doing camel case check in all classes #1385

Merged
merged 7 commits into from
Oct 5, 2023
Merged

Conversation

HaydenCognite
Copy link
Contributor

@HaydenCognite HaydenCognite commented Sep 29, 2023

Description

Dodgy attempt to address this one. Please correct me if I have misunderstood the task. :)

Checklist:

  • Tests added/updated.
  • Documentation updated. Documentation is generated from docstrings - these must be updated according to your change.
    If a new method has been added it should be referenced in cognite.rst in order to generate docs based on its docstring.
  • Changelog updated in CHANGELOG.md.
  • Version bumped. If triggering a new release is desired, bump the version number in _version.py and pyproject.toml per semantic versioning.

@codecov
Copy link

codecov bot commented Sep 29, 2023

Codecov Report

Merging #1385 (8af2452) into master (ae47d95) will decrease coverage by 0.12%.
The diff coverage is n/a.

@@            Coverage Diff             @@
##           master    #1385      +/-   ##
==========================================
- Coverage   90.59%   90.48%   -0.12%     
==========================================
  Files         117      117              
  Lines       14265    14265              
==========================================
- Hits        12924    12907      -17     
- Misses       1341     1358      +17     

see 4 files with indirect coverage changes

Copy link
Contributor

@haakonvt haakonvt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice attempt! 🥈 Let's get this over the finish line!

@@ -1,5 +1,7 @@
from pathlib import Path

ALL_FILEPATHS = Path("cognite/client/").glob("**/*.py")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just discovered there's a rglob version

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ooo! Like in this link? There is a comment in there about how apparently os.walk is faster (although this person also had ~10K files to go through. Makes me wonder what the differences are under the hood and when the payoff is worth it. But yes, will update to rglob. I can search for other globs that can be replaced rglob if you like? I will replaced any patterns like .glob("**/*.py") with .rglob("*.py"), right?

if "tests/" in str(filepath):
with filepath.open("r") as file:
for line_num, line in enumerate(file.readlines()):
if ".dump(camel_case" in line:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would skip files with arguments splatted to multiple lines. I would attack this from either AST parsing, or using the inspect module on the class objects, something along these lines:

from tests.utils import all_subclasses

for cls in all_subclasses(CogniteResource):
    if method := getattr(cls, "to_pandas"):
        if param := inspect.signature(method).parameters.get("camel_case"):
            assert param.default is False
  • add more base classes that we want to be easily dataframe-convertible.

Copy link
Contributor

@haakonvt haakonvt Oct 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we don't need getattr when we know all subclasses have this method... lol whoopsie. But for a general class, it would be beneficial ofc (with an additional third-argument None to not error when missing).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, we talked about some of this offline and on Slack (although good to document here). Just a bit busy with customer stuff but will get to it soon.

@HaydenCognite HaydenCognite marked this pull request as ready for review October 3, 2023 08:07
@HaydenCognite HaydenCognite requested review from a team as code owners October 3, 2023 08:07

@pytest.mark.parametrize("cls", [CogniteResource, CogniteResourceList])
def test_ensure_all_tests_use_camel_case_except_dump(cls):
method = "to_pandas"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems a bit excessive?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I make a variable for (almost) anything that is needed more than once. It is used three times. 🤷

def test_ensure_all_tests_use_camel_case_except_dump(cls):
method = "to_pandas"
err_msg = "Class: '{}' for method {} does not default to False."
for cls in all_subclasses(cls):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps rename the base class so that the variable names are different

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, yes, my bad.

Comment on lines 42 to 43
if cls_method := getattr(cls, method, False):
continue
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to check the logic on this one 😉

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think I noticed it before pushing, but then I got distracted and forgot to update. 😅



@pytest.mark.parametrize("cls", [CogniteResource, CogniteResourceList])
def test_ensure_all_tests_use_camel_case_except_dump(cls):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def test_ensure_all_tests_use_camel_case_except_dump(cls):
def test_ensure_all_to_pandas_methods_use_snake_case(cls):

@HaydenCognite HaydenCognite requested a review from haakonvt October 4, 2023 10:56
Copy link
Contributor

@haakonvt haakonvt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lgtm!

@HaydenCognite HaydenCognite merged commit c37e4f7 into master Oct 5, 2023
7 checks passed
@HaydenCognite HaydenCognite deleted the doge11 branch October 5, 2023 05:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants