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

use literalinclude in code-style-linting-format #259

Merged
merged 2 commits into from
May 31, 2024

Conversation

ucodery
Copy link
Collaborator

@ucodery ucodery commented May 20, 2024

No description provided.

@ucodery
Copy link
Collaborator Author

ucodery commented May 20, 2024

Tagging #250

Copy link
Member

@lwasser lwasser left a comment

Choose a reason for hiding this comment

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

@ucodery i think i just need you to help me understand how this is implemented? i think the idea is super cool. i just don't fully understand how a user will interact with the code samples and pre commit hooks in our guidebook. Thank you for this!! it's really great to have more real world examples here and frankly the stravalib examples are way to complex for our goals in this guidebook!

hooks:
- id: flake8

# Black for auto code formatting
Copy link
Member

Choose a reason for hiding this comment

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

@ucodery i've been thinking a bit about what code formatters we want to recommend. I know we suggested black and flake8 but that was last year! it seems to me a lot of people are using ruff these days! we just moved to it at stravalib a few months ago.

do you think we should consider a future issue / task for the future of suggesting ruff in place of these since it does so many things and creates a simpler / faster config?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I have also moved a few project to ruff this year. I think it is a great tool, and like hatch it enmpaases a lot of tasks that previously required a medley of tools and decisions by users, which can be great for beginners. I think we should however raise this in a new issue/ discussion.

rev: 22.12.0
hooks:
- id: black
language_version: python3.8
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
language_version: python3.8
language_version: python3.10

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I was just porting the examples as they were originally written.

@@ -36,3 +36,14 @@ docs = [
"sphinx",
"pydata-sphinx-theme"
]

[tool.ruff]
Copy link
Member

Choose a reason for hiding this comment

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

oh.. i may be confused. here you do have ruff. please help me understand this config!

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, the guide originally had examples for both black and ruff, in different sections, as it covers both. There is nothing wrong about having a pyproject.toml with both, the project will work just fine (although yes, confusing) even if the tools are set up to use conflicting settings. The tool table is just a place to stick settings. It makes no guarantees or standards about what tools are chosen or how they interact or anything.

Comment on lines +5 to +9
Parameters:
celsius (float): Temperature in Celsius.

Returns:
float: Temperature in Fahrenheit.
Copy link
Member

Choose a reason for hiding this comment

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

we prefer to use numpy style docstrings and suggest that in our guidebook.

Copy link
Member

Choose a reason for hiding this comment

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

jeremy, i think i understand what you are doing - in terms of having things in "other" formats. so you can likely ignore ALL of my inline suggestions -- BUT i don't understand how it's implemented in the guidebook. if you can give me instructions on how this would be run or how someone interacts with it that would be great!!

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I was not trying to be different, again I just ported the examples as they already existed.


def fahrenheit_to_celsius(fahrenheit):
"""
Convert temperature from Fahrenheit to Celsius.
Copy link
Member

Choose a reason for hiding this comment

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

please use numpy style here and throughout! thank you!

Copy link
Member

Choose a reason for hiding this comment

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

here are you showing other docstring formats? will those be used elsewhere in our guidebook?

Comment on lines +1 to +3
from examplePy.temperature import fahrenheit_to_celsius
import pandas
from typing import Sequence
Copy link
Member

Choose a reason for hiding this comment

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

I think if we follow isort / pep8 we'd want examplePy to be the last import?

or were you doing this intentionally to allow isort to take action is a user tries to run it?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I did this intentionally in order to make a counter example. Notice that this module is named temporal-raw (illegal python name, not allowed as an import target) while the package also has same-but ordered module named temporal.


def calc_annual_mean(df: pandas.DataFrame):
"""Function to calculate the mean temperature for each year and the final mean"""
# TODO: make this a bit more robust so we can write integration test examples??
Copy link
Member

Choose a reason for hiding this comment

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

Is this a comment from me? i kind of recall talking about tests but also we could potentially remove the todo here in the example package?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah, this is from you 😄 ec0255c4

@@ -0,0 +1,15 @@
from examplePy.temperature import fahrenheit_to_celsius
import pandas
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
import pandas
import pandas as pd

import pandas
from typing import Sequence

def calc_annual_mean(df: pandas.DataFrame):
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
def calc_annual_mean(df: pandas.DataFrame):
def calc_annual_mean(df: pd.DataFrame):

or just import DataFrame from pandas above?

from stravalib import exc
from stravalib import unithelper as uh
```
:::{literalinclude} ../examples/pure-hatch/src/examplePy/temporal.py
Copy link
Member

Choose a reason for hiding this comment

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

ahhhh i think this answers my question above about the imports. i haven't run this locally. maybe you can help me understand what will happen here in our guidebook with code examples that have formatting issues. will isort run? or would we tell the user to have it run as a small demo that they could implement?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

nothing runs, literalinclude takes the contents of some file (temporal.py) and replaces the directive with those contents somewhere between reading the md file and producing the final html. It is just a way to organize content apart from markdown files. Especially re-used content.

@lwasser
Copy link
Member

lwasser commented May 31, 2024

ok i see now @ucodery IGNORE all of my comments. this looks great. I think i have one request but i'm not sure how to implement this. What you are adding is really cool. essentially someone can test out how linters work.

i feel like we want instructions around doing this. I also feel like we could ask some of the myst folks about better ways to make it more clearly interactive. because what you have done here is really really great.

so take aways

  1. let's merge this! ignore my review. i now feel silly that i left those comments.
  2. how can we call more attention to the fact that you're adding more interactive activities to the page (which is awesome)?? -- in a separate issue we could discuss.

@ucodery
Copy link
Collaborator Author

ucodery commented May 31, 2024

A few clarifications:

These example projects are not yet full projects (I think any would build, but e.g. pure-hatch contains no python code right now). #248 is open for getting us to multiple really usable example projects.

These literalinclude PRs I've been making are just the first steps and are only about moving toml and python code that already existed in markdown into their own files; like taking a jupyter monolith and extracting some pure python modules.

I'm not trying to make any drastic changes, and I consider it a success when I make 0 changes to the rendered site after literalinclude. So although I agree that the python world has moved fast and a lot has changed in just the last year, I think those changes should be considered in separate issues.


As for how readers will interact with these examples, right now they are still just code blocks in the guide. Nothing here makes examples runnable. In order to test any of it out a reader would likely have to clone the repo and cd to the example and use CLI/ IDE to play around; they might even have to copy out the example code into a new project, to test git hooks for example.

Copy link
Member

@lwasser lwasser left a comment

Choose a reason for hiding this comment

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

thank you for this @ucodery i appreciate your patience with my (really off base review). i think i lost track of the scope of what you are doing. let's merge this!!

@lwasser lwasser merged commit 2d7402b into pyOpenSci:main May 31, 2024
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Development

Successfully merging this pull request may close these issues.

2 participants