-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Issue 4120, add reasonable exception when user try to use OnnxSequenceType attribute without specify sequence type #4272
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
Conversation
…uenceType attribute without specify sequence type
Codecov Report
@@ Coverage Diff @@
## master #4272 +/- ##
=========================================
Coverage ? 74.55%
=========================================
Files ? 878
Lines ? 154042
Branches ? 16854
=========================================
Hits ? 114851
Misses ? 34453
Partials ? 4738
|
| if (_elemType == null) | ||
| { | ||
| throw new Exception("Please specify sequence type when use OnnxSequenceType Attribute."); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please do not use braces for single line if condition #Resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually this is best practice to always use braces for if condition, in my previous team it is also suggested to always use braces for if condition to avoid further problem.
There is also some discussion online, please check out and see whether this make sense to you: https://softwareengineering.stackexchange.com/questions/16528/single-statement-if-block-braces-or-no
In reply to: 330344007 [](ancestors = 330344007)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Offline discussed with Zeeshan, this can be debatable, I will just follow the coding style in this team, not a big issue
In reply to: 330672086 [](ancestors = 330672086,330344007)
| using System.Linq; | ||
| using System.IO; | ||
| using Microsoft.ML.TestFramework.Attributes; | ||
| using System; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please place "using System" at the top and sort alphabetical order as done in source files. System libraries take preference over non-system libraries like Microsoft.ML.* #Resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
codemzs
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
![]()
| if (_elemType == null) | ||
| { | ||
| throw new Exception("Please specify sequence type when use OnnxSequenceType Attribute."); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps it is better to throw ArgumentNullException here? #Resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Throwing just a plain Exception is not recommended.
https://docs.microsoft.com/en-us/dotnet/standard/design-guidelines/using-standard-exception-types
X DO NOT throw System.Exception or System.SystemException.
However, ArgumentNullException is not correct either, since there is no argument to this method.
✓ DO throw ArgumentException or one of its subtypes if bad arguments are passed to a member.
In this case, InvalidOperationException is the most correct:
✓ DO throw an InvalidOperationException if the object is in an inappropriate state. #Resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO - it the best approach would be to remove the empty constructor and verify the Type elemType in the other constructor is never null.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, very good suggestion, InvalidOperationException is the most proper one to use here
In reply to: 330652629 [](ancestors = 330652629)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I have a similar discussion with Harish about this yesterday, we agreed that remove empty constructor seems to be more ambiguous because when our customer use [OnnxSequenceType] attribute directly he/she will get error message like "method is inaccessible due to protection level". By throwing exception we can give user more specific error message. Make sense to you?
In reply to: 330653643 [](ancestors = 330653643)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would like to take Eric's suggestion and use InvalidOperationException here.
In reply to: 330649635 [](ancestors = 330649635)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
when our customer use [OnnxSequenceType] attribute directly he/she will get error message like "method is inaccessible due to protection level"
That's a compile time error - which is exactly what we want. We want to raise the error as early as possible, so the user knows they are doing something wrong.
Also, when I get a different error message than what you are displaying:
class Program
{
[My]
private int foo;
}
class MyAttribute : Attribute
{
public MyAttribute(int required)
{ }
}Error CS7036 There is no argument given that corresponds to the required formal parameter 'required' of 'MyAttribute.MyAttribute(int)' ConsoleApp80 C:\Users\eerhardt\source\repos\ConsoleApp80\Program.cs 13 Active
Pretty obvious what to do to fix it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think Eric's suggestion is the better approach here. Please modify it as per his suggestion.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| [OnnxFact] | ||
| public void OnnxSequenceTypeWithouSpecifySequenceTypeTest() | ||
| { | ||
| Exception ex = Assert.Throws<Exception>(() => CreatePredictorWithPronlematicOutputObj()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CreatePredictorWithPronlematicOutputObj [](start = 58, length = 39)
A small typo here with Problematic #Resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| { | ||
| Exception ex = Assert.Throws<Exception>(() => CreatePredictorWithPronlematicOutputObj()); | ||
| Assert.Equal("Please specify sequence type when use OnnxSequenceType Attribute.", ex.Message); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it be better to assert on the type of exception rather than the specific message? #Resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, the exception type is assert in previous line Assert.Throws, after change to use InvalidOperationException, it should look like Assert.Throws(....), so we can assert both on exception type and exception message.
In reply to: 330651709 [](ancestors = 330651709)
harishsk
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🕐
Fixes #4120
The issue is: when user use OnnxSequenceType attribute directly without specify sequence type like [OnnxSequenceType], user will hit run time exception when try to use it.
The ideal way to fix this issue is to remove the default constructor of OnnxSequenceTypeAttribute and user will get compiler error when he/she try to use [OnnxSequenceType].
But this can be a break change in Public API. After discuss with @eerhardt and @ericstj , we decide to obsolete the default constructor for now. Then we will remove the default constructor in next major release.