-
Notifications
You must be signed in to change notification settings - Fork 587
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
Update constants
import to use module-level access #1172
#2453
Conversation
constants
import to use module-level access (#1172)constants
import to use module-level access #1172
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @WizKnight, that's it yes! Let's wait for the CI to pass and then we are good to merge this one :)
@WizKnight code quality seems to be complaining. To fix this, you must run |
The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update. |
src/huggingface_hub/hf_api.py
Outdated
from .constants import ( | ||
DEFAULT_ETAG_TIMEOUT, | ||
DEFAULT_REQUEST_TIMEOUT, | ||
DEFAULT_REVISION, | ||
DISCUSSION_STATUS, | ||
DISCUSSION_TYPES, | ||
ENDPOINT, | ||
INFERENCE_ENDPOINTS_ENDPOINT, | ||
REGEX_COMMIT_OID, | ||
REPO_TYPE_MODEL, | ||
REPO_TYPES, | ||
REPO_TYPES_MAPPING, | ||
REPO_TYPES_URL_PREFIXES, | ||
SAFETENSORS_INDEX_FILE, | ||
SAFETENSORS_MAX_HEADER_LENGTH, | ||
SAFETENSORS_SINGLE_FILE, | ||
SPACES_SDK_TYPES, | ||
WEBHOOK_DOMAIN_T, | ||
DiscussionStatusFilter, | ||
DiscussionTypeFilter, | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@WizKnight it seems this PR broke something in the tests. There are 178 failed test with the same error:
FAILED ../tests/test_file_download.py::TestNormalizeEtag::test_resolve_endpoint_on_regular_file - AttributeError: <module 'huggingface_hub.hf_api' from '/home/runner/work/huggingface_hub/huggingface_hub/src/huggingface_hub/hf_api.py'> does not have the attribute 'ENDPOINT'
Makes me realize we should keep all those previously existing constants in the module even if we don't use them. You can do that by adding
# noqa: F401 # kept for backward compatibility
to each of them. Since huggingface_hub.hf_api
is "public", we can't assume that no-one in the ecosystem has imported a constant from this module. It's unfortunately a bit ugly but we have to do that for backward compatibility.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Wauplin Yeah, it seems so and all those tests have just one common AttributeError: 'ENDPOINT'. I worked on those failed tests, I got it down to 94 failed, 189 passed, 6 skipped, 3 errors.
Though for make quality
: mypy src
Success: no issues found in 106 source files
So now how should I proceed hereafter??
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Wauplin All the tests passed!!! Just for ENDPOINT I reverted back to the previous import. It resolved the issue.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What I meant is that we should keep the imports with
from .constants import (
DEFAULT_ETAG_TIMEOUT, # noqa: F401 # kept for backward compatibility
DEFAULT_REQUEST_TIMEOUT, # noqa: F401 # kept for backward compatibility
DEFAULT_REVISION, # noqa: F401 # kept for backward compatibility
DISCUSSION_STATUS, # noqa: F401 # kept for backward compatibility
DISCUSSION_TYPES, # noqa: F401 # kept for backward compatibility
ENDPOINT, # noqa: F401 # kept for backward compatibility
INFERENCE_ENDPOINTS_ENDPOINT, # noqa: F401 # kept for backward compatibility
REGEX_COMMIT_OID, # noqa: F401 # kept for backward compatibility
REPO_TYPE_MODEL, # noqa: F401 # kept for backward compatibility
REPO_TYPES, # noqa: F401 # kept for backward compatibility
REPO_TYPES_MAPPING, # noqa: F401 # kept for backward compatibility
REPO_TYPES_URL_PREFIXES, # noqa: F401 # kept for backward compatibility
SAFETENSORS_INDEX_FILE, # noqa: F401 # kept for backward compatibility
SAFETENSORS_MAX_HEADER_LENGTH, # noqa: F401 # kept for backward compatibility
SAFETENSORS_SINGLE_FILE, # noqa: F401 # kept for backward compatibility
SPACES_SDK_TYPES, # noqa: F401 # kept for backward compatibility
WEBHOOK_DOMAIN_T, # noqa: F401 # kept for backward compatibility
DiscussionStatusFilter, # noqa: F401 # kept for backward compatibility
DiscussionTypeFilter, # noqa: F401 # kept for backward compatibility
)
Ugly? yes. But backward compatible and might save us some headaches.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, I'll add backward compatibility to the imports
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @WizKnight, I've made a new review. I'm not sure about the purpose of most of the new changes. It seems that you had some issues with your local setup. I'd advice you to install and upgrade all dev dependencies in a new virtual environment. If you have any question, please let me know.
Makefile
Outdated
python3 utils/check_inference_input_params.py | ||
python3 utils/check_contrib_list.py | ||
python3 utils/check_static_imports.py | ||
python3 utils/generate_async_inference_client.py |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These changes should not be committed. If you struggle with python version, I advice you to use a virtual environment. See installation guide for more details: https://huggingface.co/docs/huggingface_hub/installation#install-with-pip
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm using virtual environment with python 3.10 and all the required dependencies installed.
Sure I'll fix the changes
Makefile
Outdated
python3 utils/check_contrib_list.py --update | ||
python3 utils/check_static_imports.py --update | ||
python3 utils/generate_async_inference_client.py --update | ||
ruff format src/huggingface_hub/hf_api.py |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same comment here
src/huggingface_hub/__init__.py
Outdated
@@ -143,14 +143,11 @@ | |||
"CommitOperationAdd", | |||
"CommitOperationCopy", | |||
"CommitOperationDelete", | |||
"DatasetInfo", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All of these objects must not be removed from __init__.py
(ModelInfo, SpaceInfo, get_user_overview, ... as well). Can you put them back? Thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I will
src/huggingface_hub/hf_api.py
Outdated
|
||
# Optimize operations: if some files will be overwritten, we don't need to delete them first | ||
if len(add_operations) > 0: | ||
added_paths = set(op.path_in_repo for op in add_operations) | ||
delete_operations = [ | ||
delete_op for delete_op in delete_operations if delete_op.path_in_repo not in added_paths | ||
] | ||
commit_operations = delete_operations + add_operations | ||
commit_operations = cast(List[CommitOperation], delete_operations) + cast( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why that? If it's because of an issue when running make quality
locally, then you might not have the correct requirements installed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, I'll redo it from scratch. Even after that if problem persists, I'll inform you.
src/huggingface_hub/hf_api.py
Outdated
# Assuming CommitOperationBase is the common base class | ||
CommitOperation = Union[CommitOperationAdd, CommitOperationDelete] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here
src/huggingface_hub/hub_mixin.py
Outdated
if is_dataclass(cls): | ||
for key in cls.__dataclass_fields__: | ||
if is_dataclass(cls) and isinstance(cls, type): | ||
# Get type hints for the class | ||
cls_hints = get_type_hints(cls) | ||
if "__dataclass_fields__" in cls_hints: | ||
for key in cls_hints["__dataclass_fields__"]: | ||
if key not in model_kwargs and key in config: | ||
model_kwargs[key] = config[key] | ||
elif any(param.kind == inspect.Parameter.VAR_KEYWORD for param in cls._hub_mixin_init_parameters.values()): | ||
for key, value in config.items(): | ||
if key not in model_kwargs: | ||
model_kwargs[key] = value |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this is to fix an unrelated issue, better to open a separate PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was for the Error: Module "_typeshed" has no attribute "DataclassInstance"
&
Error: "Type[T]" has no attribute "__dataclass_fields__"
src/huggingface_hub/utils/_fixes.py
Outdated
@@ -92,21 +92,23 @@ def WeakFileLock(lock_file: Union[str, Path]) -> Generator[BaseFileLock, None, N | |||
|
|||
An INFO log message is emitted every 10 seconds if the lock is not acquired immediately. | |||
""" | |||
lock = FileLock(lock_file, timeout=constants.FILELOCK_LOG_EVERY_SECONDS) | |||
lock_file_str = str(lock_file) # Convert to string |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is it necessary?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For this:
src/huggingface_hub/utils/_fixes.py:95: error: Argument 1 to "UnixFileLock" has incompatible type "Union[str, Path]"; expected "str"
src/huggingface_hub/utils/_fixes.py:108: error: Incompatible types in assignment (expression has type "SoftFileLock", variable has type "UnixFileLock")
src/huggingface_hub/utils/_fixes.py:108: error: Argument 1 to "SoftFileLock" has incompatible type "Union[str, Path]"; expected "str"
src/huggingface_hub/hf_api.py
Outdated
from .constants import ( | ||
DEFAULT_ETAG_TIMEOUT, | ||
DEFAULT_REQUEST_TIMEOUT, | ||
DEFAULT_REVISION, | ||
DISCUSSION_STATUS, | ||
DISCUSSION_TYPES, | ||
ENDPOINT, | ||
INFERENCE_ENDPOINTS_ENDPOINT, | ||
REGEX_COMMIT_OID, | ||
REPO_TYPE_MODEL, | ||
REPO_TYPES, | ||
REPO_TYPES_MAPPING, | ||
REPO_TYPES_URL_PREFIXES, | ||
SAFETENSORS_INDEX_FILE, | ||
SAFETENSORS_MAX_HEADER_LENGTH, | ||
SAFETENSORS_SINGLE_FILE, | ||
SPACES_SDK_TYPES, | ||
WEBHOOK_DOMAIN_T, | ||
DiscussionStatusFilter, | ||
DiscussionTypeFilter, | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What I meant is that we should keep the imports with
from .constants import (
DEFAULT_ETAG_TIMEOUT, # noqa: F401 # kept for backward compatibility
DEFAULT_REQUEST_TIMEOUT, # noqa: F401 # kept for backward compatibility
DEFAULT_REVISION, # noqa: F401 # kept for backward compatibility
DISCUSSION_STATUS, # noqa: F401 # kept for backward compatibility
DISCUSSION_TYPES, # noqa: F401 # kept for backward compatibility
ENDPOINT, # noqa: F401 # kept for backward compatibility
INFERENCE_ENDPOINTS_ENDPOINT, # noqa: F401 # kept for backward compatibility
REGEX_COMMIT_OID, # noqa: F401 # kept for backward compatibility
REPO_TYPE_MODEL, # noqa: F401 # kept for backward compatibility
REPO_TYPES, # noqa: F401 # kept for backward compatibility
REPO_TYPES_MAPPING, # noqa: F401 # kept for backward compatibility
REPO_TYPES_URL_PREFIXES, # noqa: F401 # kept for backward compatibility
SAFETENSORS_INDEX_FILE, # noqa: F401 # kept for backward compatibility
SAFETENSORS_MAX_HEADER_LENGTH, # noqa: F401 # kept for backward compatibility
SAFETENSORS_SINGLE_FILE, # noqa: F401 # kept for backward compatibility
SPACES_SDK_TYPES, # noqa: F401 # kept for backward compatibility
WEBHOOK_DOMAIN_T, # noqa: F401 # kept for backward compatibility
DiscussionStatusFilter, # noqa: F401 # kept for backward compatibility
DiscussionTypeFilter, # noqa: F401 # kept for backward compatibility
)
Ugly? yes. But backward compatible and might save us some headaches.
Hi @WizKnight, I've checked out locally your changes to test typing issues you were having. I can't reproduce them and the CI either. I suspect that we are not on the same version (currently using CI is now passing except for unrelated errors. Time to merge! Thanks again for the contribution! |
This PR addresses issue #1172 by updating the way constants are imported and used throughout the Hugging Face Hub codebase. Instead of directly importing specific constants, we now import the entire constants module and access constants as its attributes.