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 nullability indicators for the Parse/TryParse overloads #1446

Merged
merged 2 commits into from
Dec 15, 2024

Conversation

lipchev
Copy link
Collaborator

@lipchev lipchev commented Dec 10, 2024

(UnitParser, QuantityParser, Quantity)

fixes #1445

…Parser, QuantityParser, Quantity)

- remove the invalid nullability on the Parse overloads (UnitParser)
- added nulability tests
Comment on lines +141 to +154
[Fact]
public void TryParse_WithNullAbbreviation_ReturnsFalse()
{
UnitParser unitParser = UnitsNetSetup.Default.UnitParser;
Assert.Multiple(() =>
{
var success = unitParser.TryParse(null, out LengthUnit unit);
Assert.False(success);
}, () =>
{
var success = unitParser.TryParse(null, typeof(LengthUnit), out Enum? _);
Assert.False(success);
});
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Are you Ok with this type of testing? A lot of my v6 tests use the same convention (merging only when the method name is the same).

Copy link
Owner

Choose a reason for hiding this comment

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

Absolutely, I have a very low bar for testing schemes 😄 as long as they do the job and the name of the test makes sense and kinda follows the other tests in the same area.

Copy link
Owner

Choose a reason for hiding this comment

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

Just to mention, we're using FluentAssertions at work and I do love that.

The syntax is a breeze, assertion messages way more readable and in this case you would do using var _ = new AssertionScope(); before all the assertions.

I wouldn't mind refactoring all tests over to it if I found surplus energy and time. wink wink, nudge nudge 😆

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Don't know if you've got an AI agent- but given a little bit of preparing the right prompt- it should be able to reformat a whole class without much trouble...
If you want to start us up with an example class, I might try it out (one day).

@@ -517,7 +517,7 @@ private void GenerateStaticParseMethods()
/// <example>
/// Length.Parse(""5.5 m"", CultureInfo.GetCultureInfo(""en-US""));
/// </example>
public static bool TryParse(string? str, out {_quantity.Name} result)
public static bool TryParse([NotNullWhen(true)]string? str, out {_quantity.Name} result)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is of course not necessary (and would probably increase our file size a little) but this is how the IParsable interface has it...
Haven't tried it, but I image we would get no warning in the block below:

if (Mass.TryParse(nullableStringAsInput, out _)){
  var firstChar = nullableStringAsInput[0]; // I guess this should be safe access..
}

Copy link
Owner

Choose a reason for hiding this comment

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

We should definitely do this, and seeing how widely used this is I don't imagine it increases any binary size in practice - meaning, the nuget is probably already loaded if not already bundled in the target framework.

Copy link
Owner

Choose a reason for hiding this comment

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

Okay, maybe a slight extra overhead in metadata of our methods, but still - worth it.

Copy link
Owner

@angularsen angularsen left a comment

Choose a reason for hiding this comment

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

Looks perfect, I just added a small fix

@@ -517,7 +517,7 @@ private void GenerateStaticParseMethods()
/// <example>
/// Length.Parse(""5.5 m"", CultureInfo.GetCultureInfo(""en-US""));
/// </example>
public static bool TryParse(string? str, out {_quantity.Name} result)
public static bool TryParse([NotNullWhen(true)]string? str, out {_quantity.Name} result)
Copy link
Owner

Choose a reason for hiding this comment

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

Okay, maybe a slight extra overhead in metadata of our methods, but still - worth it.

@@ -451,7 +451,7 @@ public static bool TryFrom(double value, Enum? unit, [NotNullWhen(true)] out IQu
/// <param name="quantityString">Quantity string representation, such as "1.5 kg". Must be compatible with given quantity type.</param>
/// <param name="quantity">The resulting quantity if successful, otherwise <c>default</c>.</param>
/// <returns>The parsed quantity.</returns>
public static bool TryParse(IFormatProvider? formatProvider, Type quantityType, string quantityString, [NotNullWhen(true)] out IQuantity? quantity)
public static bool TryParse(IFormatProvider? formatProvider, Type quantityType, string? quantityString, [NotNullWhen(true)] out IQuantity? quantity)
Copy link
Owner

Choose a reason for hiding this comment

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

I guess we could add [NotNullWhen(true)] to string? quantityString too.

Copy link
Owner

Choose a reason for hiding this comment

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

Seems you only edited the generated file by the way.
I'll make both changes in StaticQuantityGenerator.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This appear to have caused a compilation error in the AbbreviatedUnitsConverter - I've pushed a fix on my branch (removing the extra ?), but I don't think the PR can be reopened..

@angularsen angularsen merged commit e294419 into angularsen:release/v6 Dec 15, 2024
1 check was pending
angularsen pushed a commit that referenced this pull request Dec 15, 2024
…ild failure) (#1457)

fixing the nullability of the `unitAbbreviation` in the Parse method
(following the build failure from #1446 )

BTW, note that this method isn't even used anywhere. I'm not sure if
we've ever used it and then stopped or if it was meant to serve another
purpose (having _extended_ the converter)..
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