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

fix(torrent): get_files() should throw KeyError if files not fetched #446

Merged
merged 2 commits into from
Dec 13, 2024

Conversation

dechamps
Copy link
Contributor

@dechamps dechamps commented Jun 22, 2024

BREAKING CHANGE: torrent.get_files() will raise a KeyError if field files not fetched. file.priority and file.selected are not optional, based on fields fetched.

@trim21
Copy link
Owner

trim21 commented Jun 22, 2024

I'm considering this #422

This method should raise KeyError if files not fetched.

Copy link

codecov bot commented Jun 22, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 77.64%. Comparing base (57c064f) to head (74d862b).
Report is 57 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #446      +/-   ##
==========================================
- Coverage   77.67%   77.64%   -0.03%     
==========================================
  Files          14       14              
  Lines        1505     1503       -2     
==========================================
- Hits         1169     1167       -2     
  Misses        336      336              
Flag Coverage Δ
3.10 77.64% <100.00%> (-0.03%) ⬇️
3.11 77.64% <100.00%> (-0.03%) ⬇️
3.12 77.64% <100.00%> (-0.03%) ⬇️
3.8 77.44% <100.00%> (-0.04%) ⬇️
3.9 77.44% <100.00%> (-0.04%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@dechamps
Copy link
Contributor Author

I'm considering this #422

I commented on that PR - I don't think it makes sense to force users to fetch fields they don't need.

This method should raise KeyError if files not fetched.

I agree that would make sense. Would you like me to extend this PR to also raise KeyError if files is not present?

@trim21
Copy link
Owner

trim21 commented Jun 23, 2024

I'm considering this #422

I commented on that PR - I don't think it makes sense to force users to fetch fields they don't need.

This method should raise KeyError if files not fetched.

I agree that would make sense. Would you like me to extend this PR to also raise KeyError if files is not present?

both sounds reasonable to me. But I plan to make breaking change when tr 4.1.0 is release.

Would mind update PR and wait until tr 4.1.0 is released? I'll merge it then.

@dechamps
Copy link
Contributor Author

I plan to make breaking change when tr 4.1.0 is release

I'm not sure why you see this as a breaking change? With the previous code the only way to get get_files() to work and give the correct output was to fetch files, properties and wanted. This PR does not change the behavior in that case. It only changes the behavior for usage patterns where the previous code did not work at all in the first place. Can't break what's already broken.

Would mind update PR

I have updated the PR to make get_files() raise KeyError if files was not fetched.

@dechamps
Copy link
Contributor Author

Adjusted the PR to account for CI failures, which I didn't notice until now because it didn't run on my development branch (see #448 about that).

@dechamps dechamps requested a review from trim21 June 23, 2024 21:35
@trim21 trim21 changed the title fix(torrent): fix get_files() if properties/wanted not present fix(torrent): get_files() should throw KeyError if files not fetched Jun 24, 2024
This commit fixes the following issues with `Torrent.get_files()`:

If Client.get_torrent() was called with "files" not in `arguments`, then
`get_files()` returns an empty list. This is surprising - it should
raise an error instead.

If Client.get_torrent() was called with arguments=["files"], get_files()
fails because it expects to find `properties` and `wanted`, which were
not requested. For efficiency we should not force users to request
fields they don't need, so leave these unset in this case.
@trim21
Copy link
Owner

trim21 commented Jun 25, 2024

I plan to make breaking change when tr 4.1.0 is release

I'm not sure why you see this as a breaking change? With the previous code the only way to get get_files() to work and give the correct output was to fetch files, properties and wanted. This PR does not change the behavior in that case. It only changes the behavior for usage patterns where the previous code did not work at all in the first place. Can't break what's already broken.

obviously it will throw KeyError now

@dechamps
Copy link
Contributor Author

dechamps commented Jun 25, 2024

obviously it will throw KeyError now

It will not throw KeyError in code that is not already broken.

If a caller called this method without fetching files first, the previous code would return an incorrect result (i.e. an empty list). Meaning it's already broken.

The new code takes this already broken case and breaks it in a different, clearer way, by raising an error.

Caller code that is correct (i.e. callers who fetch files before calling get_files()) will not be broken by this change. The change only breaks callers that are already broken in the first place.

@trim21
Copy link
Owner

trim21 commented Jun 25, 2024

obviously it will throw KeyError now

It will not throw KeyError in code that is not already broken.

If a caller called this method without fetching files first, the previous code would return an incorrect result (i.e. an empty list). Meaning it's already broken.

The new code takes this already broken case and breaks it in a different, clearer way, by raising an error.

Caller code that is correct (i.e. callers who fetch files before calling get_files()) will not be broken by this change. The change only breaks callers that are already broken in the first place.

I get your point. To be honest, I had same conversation in some other repos with their maintainer.

And yes, you are right. calling .files() without files field fetched is actually not valid call, but changing it to raise a execption will have control flow problem.

Consider a long running script just call files and iter it, without try/catch.

It may get broken by this change, and I want to avoid this.

Also, there is already a in-plan breaking change so make a change like this in a non-major version change sounds like a realy bad idea to me.

@trim21 trim21 merged commit 952e4e8 into trim21:master Dec 13, 2024
20 of 21 checks passed
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