Other answers did not work for me, but I found another solution.
I installed Xcode, and then the upgrade succeeded.
Additional info:
Environment: MacBook Pro 2016, macOS Monterey
Using `brew upgrade glib -v`, I found that `/usr/bin/python3` was used during the `gobject-introspection` build.
Things I tried (but failed):
- Changed `PATH` to prioritize `/usr/local/bin/python3`
- Updated `PKG_CONFIG_PATH` to include `/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/pkgconfig`
Checked `python-3.9.pc`, which had 'prefix=/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.9' so,
- Changed it to 'prefix=/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9'
Since other formulae required Xcode, I installed Xcode, and that resolved the issue.
One more thing I haven't tried yet:
/usr/bin/python3 -m pip install pkgconfig
might also help.