Skip to content

Commit c6c0003

Browse files
Modernization.
1 parent 2ab167a commit c6c0003

File tree

4 files changed

+71
-63
lines changed

4 files changed

+71
-63
lines changed

.github/workflows/lint.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
run: pip install black==22.6.0 flake8==5.0.4
1919

2020
- name: Run linters
21-
uses: samuelmeuli/lint-action@v1.5.3
21+
uses: wearerequired/lint-action
2222
with:
2323
github_token: ${{ secrets.github_token }}
2424
# Enable linters

.pre-commit-config.yaml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,24 @@ repos:
44
# Sort imports prior to black reformatting, to
55
# ensure black always takes prescedence
66
- repo: https://github.com/timothycrosley/isort
7-
rev: 4.3.21
7+
rev: 6.0.1
88
hooks:
99
- id: isort
1010
- repo: https://github.com/ambv/black
11-
rev: 22.6.0
11+
rev: 25.1.0
1212
hooks:
1313
- id: black
1414
- repo: https://github.com/pycqa/flake8
15-
rev: 5.0.4
15+
rev: 7.1.2
1616
hooks:
1717
- id: flake8
1818
- repo: https://github.com/asottile/pyupgrade
19-
rev: v1.25.1
19+
rev: v3.19.1
2020
hooks:
2121
- id: pyupgrade
22-
args: ["--py37-plus"]
22+
args: ["--py39-plus"]
2323
- repo: https://github.com/pre-commit/mirrors-mypy
24-
rev: v1.7.1
24+
rev: v1.15.0
2525
hooks:
2626
- id: mypy
2727
args:

myfitnesspal/client.py

Lines changed: 52 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from collections import OrderedDict
99
from http.cookiejar import CookieJar
1010
from pathlib import Path
11-
from typing import Any, Dict, List, Optional, Union, cast, overload
11+
from typing import Any, cast, overload
1212
from urllib import parse
1313

1414
import browser_cookie3
@@ -64,7 +64,7 @@ class Client(MFPBase):
6464

6565
def __init__(
6666
self,
67-
cookiejar: Optional[CookieJar] = None,
67+
cookiejar: CookieJar | None = None,
6868
unit_aware: bool = False,
6969
log_requests_to: Path | None = None,
7070
):
@@ -97,7 +97,7 @@ def __init__(
9797
self._user_metadata = self._get_user_metadata()
9898

9999
@property
100-
def user_id(self) -> Optional[types.MyfitnesspalUserId]:
100+
def user_id(self) -> types.MyfitnesspalUserId | None:
101101
"""The user_id of the logged-in account."""
102102
if self._auth_data is None:
103103
return None
@@ -110,7 +110,7 @@ def user_metadata(self) -> types.UserMetadata:
110110
return self._user_metadata
111111

112112
@property
113-
def access_token(self) -> Optional[str]:
113+
def access_token(self) -> str | None:
114114
"""The access token for the logged-in account."""
115115
if self._auth_data is None:
116116
return None
@@ -222,7 +222,7 @@ def _get_request_for_url(
222222
self,
223223
url: str,
224224
send_token: bool = False,
225-
headers: Optional[Dict[str, str]] = None,
225+
headers: dict[str, str] | None = None,
226226
**kwargs,
227227
) -> requests.Response:
228228
request_id = uuid.uuid4()
@@ -292,7 +292,7 @@ def _get_json_for_url(self, url):
292292

293293
return json.loads(content)
294294

295-
def _get_measurement(self, name: str, value: Optional[float]) -> MeasureBase:
295+
def _get_measurement(self, name: str, value: float | None) -> MeasureBase:
296296
if not self.unit_aware:
297297
return value
298298
measure, kwarg = self.DEFAULT_MEASURE_AND_UNIT[name]
@@ -357,7 +357,7 @@ def _get_completion(self, document) -> bool:
357357

358358
return False # Who knows, probably not my diary.
359359

360-
def _get_meals(self, document) -> List[Meal]:
360+
def _get_meals(self, document) -> list[Meal]:
361361
meals = []
362362
fields = None
363363
meal_headers = document.xpath("//tr[@class='meal_header']")
@@ -514,12 +514,10 @@ def _extract_value(self, element):
514514
return value
515515

516516
@overload
517-
def get_date(self, year: int, month: int, day: int) -> Day:
518-
...
517+
def get_date(self, year: int, month: int, day: int) -> Day: ...
519518

520519
@overload
521-
def get_date(self, date: datetime.date) -> Day:
522-
...
520+
def get_date(self, date: datetime.date) -> Day: ...
523521

524522
def get_date(self, *args, **kwargs) -> Day:
525523
"""Returns your meal diary for a particular date"""
@@ -600,9 +598,9 @@ def _ensure_upper_lower_bound(self, lower_bound, upper_bound):
600598
def get_measurements(
601599
self,
602600
measurement="Weight",
603-
lower_bound: Optional[datetime.date] = None,
604-
upper_bound: Optional[datetime.date] = None,
605-
) -> Dict[datetime.date, float]:
601+
lower_bound: datetime.date | None = None,
602+
upper_bound: datetime.date | None = None,
603+
) -> dict[datetime.date, float]:
606604
"""Returns measurements of a given name between two dates."""
607605
upper_bound, lower_bound = self._ensure_upper_lower_bound(
608606
lower_bound, upper_bound
@@ -654,8 +652,8 @@ def get_measurements(
654652
def set_measurements(
655653
self,
656654
measurement="Weight",
657-
value: Optional[float] = None,
658-
date: Optional[datetime.date] = None,
655+
value: float | None = None,
656+
date: datetime.date | None = None,
659657
) -> None:
660658
"""Sets measurement for today's date."""
661659
if value is None:
@@ -734,7 +732,7 @@ def _get_measurements(self, document):
734732

735733
return measurements_dict
736734

737-
def _get_measurement_ids(self, document) -> Dict[str, int]:
735+
def _get_measurement_ids(self, document) -> dict[str, int]:
738736
ids = {}
739737
for next_data in document.xpath("//script[@id='__NEXT_DATA__']"):
740738
next_data_json = json.loads(next_data.text)
@@ -758,7 +756,7 @@ def _get_notes(self, date: datetime.date) -> Note:
758756
)
759757
return Note(result.json()["item"])
760758

761-
def _get_water(self, date: datetime.date) -> Union[float, Volume]:
759+
def _get_water(self, date: datetime.date) -> float | Volume:
762760
result = self._get_request_for_url(
763761
parse.urljoin(
764762
self.BASE_URL_SECURE,
@@ -776,9 +774,9 @@ def get_report(
776774
self,
777775
report_name: str = "Net Calories",
778776
report_category: str = "Nutrition",
779-
lower_bound: Optional[datetime.date] = None,
780-
upper_bound: Optional[datetime.date] = None,
781-
) -> Dict[datetime.date, float]:
777+
lower_bound: datetime.date | None = None,
778+
upper_bound: datetime.date | None = None,
779+
) -> dict[datetime.date, float]:
782780
"""
783781
Returns report data of a given name and category between two dates.
784782
"""
@@ -818,15 +816,18 @@ def _get_url_for_report(
818816
return (
819817
parse.urljoin(
820818
self.BASE_URL_SECURE,
821-
"api/services/reports/results/" + report_category.lower() + "/" + report_name,
819+
"api/services/reports/results/"
820+
+ report_category.lower()
821+
+ "/"
822+
+ report_name,
822823
)
823824
+ f"/{str(delta.days)}.json"
824825
)
825826

826-
def _get_report_data(self, json_data: dict) -> Dict[datetime.date, float]:
827-
report_data: Dict[datetime.date, float] = {}
827+
def _get_report_data(self, json_data: dict) -> dict[datetime.date, float]:
828+
report_data: dict[datetime.date, float] = {}
828829

829-
data = json_data.get("outcome").get("results")
830+
data = json_data.get("outcome", {}).get("results")
830831

831832
if not data:
832833
return report_data
@@ -848,7 +849,7 @@ def _get_report_data(self, json_data: dict) -> Dict[datetime.date, float]:
848849
def __str__(self) -> str:
849850
return f"MyFitnessPal Client for {self.effective_username}"
850851

851-
def get_food_search_results(self, query: str) -> List[FoodItem]:
852+
def get_food_search_results(self, query: str) -> list[FoodItem]:
852853
"""Search for foods matching a specified query."""
853854
search_url = parse.urljoin(self.BASE_URL_SECURE, self.SEARCH_PATH)
854855
document = self._get_document_for_url(search_url)
@@ -875,7 +876,7 @@ def get_food_search_results(self, query: str) -> List[FoodItem]:
875876

876877
return self._get_food_search_results(document)
877878

878-
def _get_food_search_results(self, document) -> List[FoodItem]:
879+
def _get_food_search_results(self, document) -> list[FoodItem]:
879880
item_divs = document.xpath("//li[@class='matched-food']")
880881

881882
items = []
@@ -970,19 +971,19 @@ def set_new_food(
970971
fat: float,
971972
carbs: float,
972973
protein: float,
973-
sodium: Optional[float] = None,
974-
potassium: Optional[float] = None,
975-
saturated_fat: Optional[float] = None,
976-
polyunsaturated_fat: Optional[float] = None,
977-
fiber: Optional[float] = None,
978-
monounsaturated_fat: Optional[float] = None,
979-
sugar: Optional[float] = None,
980-
trans_fat: Optional[float] = None,
981-
cholesterol: Optional[float] = None,
982-
vitamin_a: Optional[float] = None,
983-
calcium: Optional[float] = None,
984-
vitamin_c: Optional[float] = None,
985-
iron: Optional[float] = None,
974+
sodium: float | None = None,
975+
potassium: float | None = None,
976+
saturated_fat: float | None = None,
977+
polyunsaturated_fat: float | None = None,
978+
fiber: float | None = None,
979+
monounsaturated_fat: float | None = None,
980+
sugar: float | None = None,
981+
trans_fat: float | None = None,
982+
cholesterol: float | None = None,
983+
vitamin_a: float | None = None,
984+
calcium: float | None = None,
985+
vitamin_c: float | None = None,
986+
iron: float | None = None,
986987
serving_size: str = "1 Serving",
987988
servingspercontainer: float = 1.0,
988989
sharepublic: bool = False,
@@ -1104,12 +1105,12 @@ def set_new_goal(
11041105
self,
11051106
energy: float,
11061107
energy_unit: str = "calories",
1107-
carbohydrates: Optional[float] = None,
1108-
protein: Optional[float] = None,
1109-
fat: Optional[float] = None,
1110-
percent_carbohydrates: Optional[float] = None,
1111-
percent_protein: Optional[float] = None,
1112-
percent_fat: Optional[float] = None,
1108+
carbohydrates: float | None = None,
1109+
protein: float | None = None,
1110+
fat: float | None = None,
1111+
percent_carbohydrates: float | None = None,
1112+
percent_protein: float | None = None,
1113+
percent_fat: float | None = None,
11131114
) -> None:
11141115
"""Updates your nutrition goals.
11151116
@@ -1259,7 +1260,7 @@ def set_new_goal(
12591260
"status code: {status}".format(status=result.status_code)
12601261
)
12611262

1262-
def get_recipes(self) -> Dict[int, str]:
1263+
def get_recipes(self) -> dict[int, str]:
12631264
"""Returns a dictionary with all saved recipes.
12641265
12651266
Recipe ID will be used as dictionary key, recipe title as dictionary value.
@@ -1313,7 +1314,7 @@ def get_recipe(self, recipeid: int) -> types.Recipe:
13131314
recipe_url = parse.urljoin(self.BASE_URL_SECURE, recipe_path)
13141315
document = self._get_document_for_url(recipe_url)
13151316

1316-
recipe_dict: Dict[str, Any] = {
1317+
recipe_dict: dict[str, Any] = {
13171318
"@context": "https://schema.org",
13181319
"@type": "Recipe",
13191320
"author": self.effective_username,
@@ -1372,7 +1373,7 @@ def get_recipe(self, recipeid: int) -> types.Recipe:
13721373
recipe_dict["tags"] = ["MyFitnessPal"]
13731374
return cast(types.Recipe, recipe_dict)
13741375

1375-
def get_meals(self) -> Dict[int, str]:
1376+
def get_meals(self) -> dict[int, str]:
13761377
"""Returns a dictionary with all saved meals.
13771378
13781379
Key: Meal ID
@@ -1386,7 +1387,7 @@ def get_meals(self) -> Dict[int, str]:
13861387
meals = document.xpath(
13871388
"//*[@id='matching']/li"
13881389
) # get all items in the recipe list
1389-
_idx: Optional[int] = None
1390+
_idx: int | None = None
13901391
try:
13911392
for _idx, meal in enumerate(meals):
13921393
meal_path = meal.xpath("./a")[0].attrib["href"]
@@ -1409,7 +1410,7 @@ def get_meal(self, meal_id: int, meal_title: str) -> types.Recipe:
14091410
meal_url = parse.urljoin(self.BASE_URL_SECURE, meal_path)
14101411
document = self._get_document_for_url(meal_url)
14111412

1412-
recipe_dict: Dict[str, Any] = {
1413+
recipe_dict: dict[str, Any] = {
14131414
"@context": "https://schema.org",
14141415
"@type": "Recipe",
14151416
"author": self.effective_username,

setup.cfg

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,18 @@
22
# https://github.com/ambv/black#line-length
33
max-line-length = 88
44
ignore =
5-
E203, # E203: whitespace before ':' (defer to black)
6-
E231, # E231: missing whitespace after ',' (defer to black)
7-
E501, # E501: line length (defer to black)
8-
W503, # W503: break before binary operators (defer to black)
9-
A003, # A003: [builtins] allow class attributes to be named after builtins (e.g., `id`)
5+
# E203: whitespace before ':' (defer to black)
6+
E203,
7+
# E231: missing whitespace after ',' (defer to black)
8+
E231,
9+
# E501: line length (defer to black)
10+
E501,
11+
# W503: break before binary operators (defer to black)
12+
W503,
13+
# A003: [builtins] allow class attributes to be named after builtins (e.g., `id`)
14+
A003,
15+
# E704: Allow multiple statements on one line
16+
E704,
1017

1118
[pep8]
1219
max-line-length = 88

0 commit comments

Comments
 (0)