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

findSolid errors #655

Merged
merged 6 commits into from
Mar 3, 2021
Merged

Conversation

marcus7070
Copy link
Member

Workplane.findSolid had an incorrect docstring about it raising a ValueError when it didn't find any solids. It actually just returns None, and this is behaviour that a few other methods rely on (union for example). So I removed the line in the docstring.

There were several methods that treated the return from findSolid as just a Solid, and hence were trying to call methods on the None object and failing with an AttributeError. I added checks for None and ValueError exceptions to these methods.

Then I thought to myself, "This is mypy's job, why am I doing it?", and realised that findSolid needed the type hint Optional added to it's return value.

I also added tests for all the new exceptions. I realised cutEach isn't tested at all (except for probably in the examples), so I added a basic test for that as well.

Removed a line in the findSolid docstring about raising an error if no
solids found. Added exceptions to several methods that were previously
failing with AttributeErrors when they tried to treat the None object as
a solid.
@codecov
Copy link

codecov bot commented Feb 24, 2021

Codecov Report

Merging #655 (d711a81) into master (e4ed881) will increase coverage by 0.16%.
The diff coverage is 97.14%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #655      +/-   ##
==========================================
+ Coverage   94.03%   94.19%   +0.16%     
==========================================
  Files          31       31              
  Lines        6636     6683      +47     
  Branches      726      725       -1     
==========================================
+ Hits         6240     6295      +55     
+ Misses        258      253       -5     
+ Partials      138      135       -3     
Impacted Files Coverage Δ
cadquery/cq.py 90.80% <95.00%> (+0.90%) ⬆️
tests/test_cadquery.py 99.09% <98.00%> (-0.04%) ⬇️
cadquery/occ_impl/shapes.py 91.89% <0.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update e4ed881...d711a81. Read the comment docs.

@marcus7070
Copy link
Member Author

I forgot about the Workplane.findFace method, if I've added Optional to findSolid I should do it for findFace as well.

@adam-urbanczyk
Copy link
Member

Thanks for picking this up. Open question: isn't it better if findSolid raises? This would result in less code duplication.

@marcus7070
Copy link
Member Author

isn't it better if findSolid raises? This would result in less code duplication.

Firstly, if I had of realised how large this PR was going to be I would have made an issue first and discussed this there. But honestly I was intending to take a 10 minute break from work and fix one line in the findSolid docstring and it just snowballed from there. 😅

I thought about raising an exception in findSolid, but:

  • that's a user facing method and it's had the behaviour of returning None for 8 years now, and
  • it seemed like the smaller change to add a few raise ValueErrors than to change the behaviour of findSolid.

On the other hand:

  • if no user has complained about the error in the docstring for 8 years, it's probably rarely used and a change won't be too disruptive,
  • raising in findSolid would bring it in line with how I intend to do getPending in Improved error when pendingWires/pendingEdges is empty #621 (which I'm also working on now),
  • it turned out to be more than just a few raise ValueErrors, so raising in findSolid would reduce a lot of duplication,
  • at present findSolid is just an alias for Workplane._findType((Solid, Compound)), so it will be trivial to work around if you need the old behaviour.

Now I've written all that out, I prefer raising in findSolid. @adam-urbanczyk, @jmwright, are you OK with me changing the behaviour of findSolid to raise an exception instead of returning None?

@marcus7070 marcus7070 marked this pull request as draft February 24, 2021 10:48
@jmwright
Copy link
Member

are you OK with me changing the behaviour of findSolid to raise an exception instead of returning None?

I am, yes.

@adam-urbanczyk
Copy link
Member

+1

@marcus7070 marcus7070 marked this pull request as ready for review February 25, 2021 06:02
@marcus7070
Copy link
Member Author

Workplane.largestDimension previously returned -1 when it couldn't find a solid/compound. Now it also raises a ValueError.

Copy link
Member

@jmwright jmwright left a comment

Choose a reason for hiding this comment

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

@marcus7070 Looks good, thanks!

tests/test_cadquery.py Show resolved Hide resolved
@adam-urbanczyk adam-urbanczyk merged commit 0325474 into CadQuery:master Mar 3, 2021
@adam-urbanczyk
Copy link
Member

Thanks @marcus7070

@marcus7070 marcus7070 deleted the findSolid-errors branch March 4, 2021 01:12
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.

3 participants