Create and use attribute getter macros

Fixes #1931

I'll open a separate PR to use these macros elsewhere once this merges.