Skip to content

Commit 4a0efca

Browse files
committed
Add mypy, remove =None for non-optional fields
Poetry update
1 parent 8192f7f commit 4a0efca

File tree

13 files changed

+511
-438
lines changed

13 files changed

+511
-438
lines changed

.github/workflows/ci.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,8 @@ jobs:
4343
- name: Lint code with ruff
4444
run: poetry run ruff check .
4545

46+
- name: Lint code with mypy
47+
run: poetry run mypy rss_parser
48+
4649
- name: Test code with pytest
4750
run: poetry run pytest --doctest-modules

.pre-commit-config.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,16 @@ repos:
2424
types: [ python ]
2525
stages: [ commit ]
2626

27+
- id: mypy-check-staged
28+
name: mypy
29+
entry: poetry
30+
args:
31+
- run
32+
- mypy
33+
- rss_parser
34+
language: system
35+
stages: [ push ]
36+
2737
- id: ruff-check-global
2838
name: ruff
2939
entry: poetry

poetry.lock

Lines changed: 464 additions & 413 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,16 @@ packages = [{ include = "rss_parser" }, { include = "rss_parser/py.typed" }]
4141
python = "^3.9"
4242
pydantic = ">1.9"
4343
xmltodict = "^0.13.0"
44+
types-xmltodict = "^0.14.0.20241009"
4445

4546
[tool.poetry.group.dev.dependencies]
4647
ipython = "*"
47-
black = "^22.3.0"
48+
black = "*"
4849
pre-commit = "^2.12.0"
4950
ruff = "*"
5051
rich = "*"
5152
pytest = "^7.4.0"
53+
mypy = "*"
5254

5355
[tool.pytest.ini_options]
5456
addopts = "--color=yes"
@@ -79,7 +81,6 @@ select = [
7981
"EXE", # flake8-executable
8082
"T20", # flake8-print
8183
"TID", # flake8-tidy-imports
82-
"TCH", # flake8-type-checking
8384
"ARG", # flake8-unused-arguments
8485
"RUF", # ruff
8586
]

rss_parser/models/__init__.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,17 @@
33
44
Some types and validation may be a bit custom to account for broken standards in some RSS feeds.
55
"""
6+
67
from json import loads
8+
from typing import TYPE_CHECKING
79

810
from rss_parser.models.utils import camel_case
911
from rss_parser.pydantic_proxy import import_v1_pydantic
1012

11-
pydantic = import_v1_pydantic()
13+
if TYPE_CHECKING:
14+
from pydantic import v1 as pydantic
15+
else:
16+
pydantic = import_v1_pydantic()
1217

1318

1419
class XMLBaseModel(pydantic.BaseModel):
@@ -19,7 +24,7 @@ def json_plain(self, **kw):
1924
"""
2025
Run pydantic's json with custom encoder to encode Tags as only content.
2126
"""
22-
from rss_parser.models.types.tag import Tag
27+
from rss_parser.models.types.tag import Tag # noqa: PLC0415
2328

2429
return self.json(models_as_dict=False, encoder=Tag.flatten_tag_encoder, **kw)
2530

rss_parser/models/rss/channel.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@
1515
class RequiredChannelElementsMixin(XMLBaseModel):
1616
"""https://www.rssboard.org/rss-specification#requiredChannelElements."""
1717

18-
title: Tag[str] = None # GoUpstate.com News Headlines
18+
title: Tag[str] # GoUpstate.com News Headlines
1919
"The name of the channel. It's how people refer to your service. If you have an HTML website that contains " "the same information as your RSS file, the title of your channel should be the same as the title of your " "website." # noqa
2020

21-
link: Tag[str] = None # http://www.goupstate.com/
21+
link: Tag[str] # http://www.goupstate.com/
2222
"The URL to the HTML website corresponding to the channel."
2323

24-
description: Tag[str] = None # The latest news from GoUpstate.com, a Spartanburg Herald-Journal Web site.
24+
description: Tag[str] # The latest news from GoUpstate.com, a Spartanburg Herald-Journal Web site.
2525
"Phrase or sentence describing the channel."
2626

2727

@@ -33,7 +33,7 @@ class OptionalChannelElementsMixin(XMLBaseModel):
3333
language: Optional[Tag[str]] = None # en-us
3434
"The language the channel is written in. This allows aggregators to group all Italian language sites, " "for example, on a single page." # noqa
3535

36-
copyright: Optional[Tag[str]] = None # Copyright 2002, Spartanburg Herald-Journal # noqa
36+
copyright: Optional[Tag[str]] = None # Copyright 2002, Spartanburg Herald-Journal
3737
"Copyright notice for content in the channel."
3838

3939
"Email address for person responsible for editorial content."

rss_parser/models/rss/image.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77
class Image(XMLBaseModel):
88
"""https://www.rssboard.org/rss-specification#ltimagegtSubelementOfLtchannelgt."""
99

10-
url: Tag[str] = None
10+
url: Tag[str]
1111
"The URL of a GIF, JPEG or PNG image that represents the channel."
1212

13-
title: Tag[str] = None
13+
title: Tag[str]
1414
"Describes the image, it's used in the ALT attribute of the HTML <img> tag when the channel is rendered in HTML."
1515

16-
link: Tag[str] = None
16+
link: Tag[str]
1717
"The URL of the site, when the channel is rendered, the image is a link to the site. (Note, in practice the " "image <title> and <link> should have the same value as the channel's <title> and <link>." # noqa
1818

1919
width: Optional[Tag[int]] = None

rss_parser/models/rss/item.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,13 @@
99

1010

1111
class RequiredItemElementsMixin(XMLBaseModel):
12-
title: Tag[str] = None # Venice Film Festival Tries to Quit Sinking
12+
title: Tag[str] # Venice Film Festival Tries to Quit Sinking
1313
"The title of the item."
1414

1515
links: OnlyList[Tag[str]] = pydantic.Field(alias="link") # http://nytimes.com/2004/12/07FEST.html
1616
"The URL of the item."
1717

18-
description: Tag[
19-
str
20-
] = None # <description>Some of the most heated chatter at the Venice Film Festival this week was
18+
description: Tag[str] # <description>Some of the most heated chatter at the Venice Film Festival this week was
2119
# about the way that the arrival of the stars at the Palazzo del Cinema was being staged.</description>
2220
"The item synopsis."
2321

rss_parser/models/rss/text_input.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ class TextInput(XMLBaseModel):
1010
https://www.rssboard.org/rss-specification#lttextinputgtSubelementOfLtchannelgt
1111
"""
1212

13-
title: Tag[str] = None
13+
title: Tag[str]
1414
"The label of the Submit button in the text input area."
1515

16-
description: Tag[str] = None
16+
description: Tag[str]
1717
"Explains the text input area."
1818

19-
name: Tag[str] = None
19+
name: Tag[str]
2020
"The name of the text object in the text input area."
2121

22-
link: Tag[str] = None
22+
link: Tag[str]
2323
"The URL of the CGI script that processes text input requests."

rss_parser/models/types/date.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from datetime import datetime
22
from email.utils import parsedate_to_datetime
3+
from typing import Union
34

45
from rss_parser.pydantic_proxy import import_v1_pydantic
56

@@ -25,7 +26,7 @@ def __repr__(self):
2526
return f"DateTimeOrStp({super().__repr__()})"
2627

2728

28-
def validate_dt_or_str(value: str) -> datetime:
29+
def validate_dt_or_str(value: str) -> Union[datetime, str]:
2930
# Try to parse standard (RFC 822)
3031
try:
3132
return parsedate_to_datetime(value)

0 commit comments

Comments
 (0)