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

font.kerning.get() unnecessarily restricts types for default return value #723

Open
BoldMonday opened this issue Feb 20, 2024 · 2 comments

Comments

@BoldMonday
Copy link

font.kerning.get() allows for a default return value in case the pair is not present.

However, this afternoon I discovered that fontParts puts an unnecessary restriction on what types it allows for the default value.
Where in former days of RoboFab the code below would happily work:

f = CurrentFont()
print(f.kerning.get(('A', 'Y'), 'no value found'))

Now I was suddenly confronted with this cryptic traceback:

  File "/Applications/RoboFont-4.2.app/Contents/Resources/lib/python3.7/fontParts/base/kerning.py", line 350, in get
  File "/Applications/RoboFont-4.2.app/Contents/Resources/lib/python3.7/fontParts/base/base.py", line 361, in get
  File "/Applications/RoboFont-4.2.app/Contents/Resources/lib/python3.7/fontParts/base/normalizers.py", line 152, in normalizeKerningValue
TypeError: Kerning value must be a int or a float, not str.

To me this seems an overly strict application of normalisation.
Just like in standard Python dictionaries, default return values should be passed along as they are.

@typemytype
Copy link
Member

according the docs it should either be a kern value or None

the fallback you can provide can also only be a int/float or None, nothing else...

I guess its best to test on None: print(c.kerning.get(("a", "b")) or "no value found")

https://fontparts.robotools.dev/en/stable/objectref/objects/kerning.html#fontParts.base.BaseKerning.find

Returns the value for the kerning pair. pair is a tuple of two Strings, and the returned values will either be Integer/Float or None if no pair was found.

@BoldMonday
Copy link
Author

BoldMonday commented Feb 21, 2024

Thanks for the explanation and the help.

I guess its best to test on None: print(c.kerning.get(("a", "b")) or "no value found")

This will not work when the pair in question has a kerning value of 0 unfortunately.
I solved it with a more elaborate if condition instead.

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

No branches or pull requests

2 participants