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

types #143

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open

types #143

wants to merge 5 commits into from

Conversation

yagebu
Copy link
Member

@yagebu yagebu commented Jan 3, 2025

  • adjust types for a successful mypy run
  • ci: add mypy ci step

@yagebu yagebu force-pushed the types branch 4 times, most recently from c4138e4 to 0b55392 Compare January 3, 2025 08:06
@yagebu yagebu marked this pull request as ready for review January 3, 2025 08:18
@yagebu yagebu mentioned this pull request Jan 3, 2025
Copy link
Collaborator

@dnicolodi dnicolodi left a comment

Choose a reason for hiding this comment

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

I'm not convinced that adding types to the examples is a good idea. The examples are supposed to be as simple as possible and serve as templates for inexperienced programmers. Adding typing annotations seems to only add complexity for very little gain.

- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: '3.9'
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why Python 3.9?

Copy link
Member Author

Choose a reason for hiding this comment

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

that's the oldest supported version of Beancount - which version do you want?

@@ -155,7 +155,7 @@ def get_file(filename):
return _CACHE[filename]


_CACHE = utils.DefaultDictWithKey(_FileMemo)
_CACHE = utils.DefaultDictWithKey(_FileMemo) # type: ignore
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why is the ignore needed?

Copy link
Member Author

Choose a reason for hiding this comment

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

DefaultDictWithKey is not typed correctly - it inherits the types from defaultdict but takes a default function which takes one argument. Shall I add a comment explaining that?

@@ -5,7 +5,7 @@


class Importer(csvbase.Importer):
date = csvbase.Date('Posting Date', '%m/%d/%Y')
date = csvbase.Date('Posting Date', '%m/%d/%Y') # type: ignore
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why?

Copy link
Member Author

Choose a reason for hiding this comment

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

This date column shadows the date function of the importer protocol. Seems like the base class initialisation makes that all work but I don't know if this can be typed correctly.

index = 0
with open(filepath) as infile:
for index, row in enumerate(csv.DictReader(infile)):
meta = data.new_metadata(filepath, index)
date = parse(row['DATE']).date()
rtype = row['TYPE']
link = f"ut{row['REF #']}"
links = frozenset([link])
Copy link
Collaborator

Choose a reason for hiding this comment

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

These two lines can be one line.

@@ -122,9 +123,9 @@ def extract(self, filepath, existing):
rate = D(match.group(3))

if rtype == 'BUY':
cost = position.Cost(rate, self.currency, None, None)
cost = position.CostSpec(rate, None, self.currency, None, None, None)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why? Cost should be just fine.

Copy link
Member Author

Choose a reason for hiding this comment

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

a Cost requires a date (the booking code will insert it for all postings, falling back to the entry date)

@@ -143,11 +144,11 @@ def extract(self, filepath, existing):
logging.error("Missing cost basis in '%s'", row['DESCRIPTION'])
continue
cost_number = D(match.group(1))
cost = position.Cost(cost_number, self.currency, None, None)
cost = position.CostSpec(cost_number, None, self.currency, None, None, None)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Here too.

links.add(link)
tag = getattr(rec, "tag", None)
if tag:
tags.add(tag)
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm not entirely convinced that the type for links and tags should be frozenset and not simply collections.abc.Set. Anyhow, This seems a very convoluted way to crete a frozenset of one element.

Copy link
Member Author

Choose a reason for hiding this comment

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

I stuck to the original code but I'm happy to refactor this as well

Some type: ignores were added but only internal uses of functionality.
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