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

hasproperty(Base, ::Symbol) seems to give incorrect results for non-exported names #46857

Closed
DilumAluthge opened this issue Sep 22, 2022 · 5 comments

Comments

@DilumAluthge
Copy link
Member

  | | |_| | | | (_| |  |  Version 1.9.0-DEV.1423 (2022-09-21)
 _/ |\__'_|_|_|\__'_|  |  Commit 62ac26a4b06 (0 days old master)
|__/                   |

julia> getproperty(Base, :_cat)
_cat (generic function with 9 methods)

julia> hasproperty(Base, :_cat)
false
@DilumAluthge DilumAluthge added the bug Indicates an unexpected problem or unintended behavior label Sep 22, 2022
@JeffBezanson
Copy link
Member

I think this was intentional. We wanted propertynames to only list exported names, and hasproperty to be consistent with that.

@DilumAluthge DilumAluthge removed the bug Indicates an unexpected problem or unintended behavior label Sep 22, 2022
@DilumAluthge
Copy link
Member Author

Ah. In that case, is there a way that I can tell whether or not getproperty will succeed without actually calling it? Right now, I'm just doing a try-catch.

@mikmoore
Copy link
Contributor

mikmoore commented Sep 23, 2022

It seems to me that hasproperty should match the behavior of getproperty, as nobody want so rely on try-catch to actually check these things. propertynames has a private argument to determine whether private properties should be returned, but it seems there is no similar option for hasproperty. Even if there were, it would seem that the sensible default would be for hasproperty to include private properties and only restrict itself to public properties by an explicit argument, since it seems that the primary purpose is to avoid invalid getproperty calls.

@DilumAluthge
Copy link
Member Author

Kristoffer suggested using isdefined(Base, :foo) for this, and it seems to work perfectly. That is, in my brief experiments, it seems that isdefined(Base, sym) is true if and only if getproperty(Base, sym) does not throw.

So I'm inclined to close this as resolved.

@vtjnash
Copy link
Member

vtjnash commented Sep 23, 2022

That is much more accurate too. Since something can have a property, but still throw UndefRef on access

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

4 participants