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

Global regex configuration not inherited in sub-sections #64

Closed
kdeldycke opened this issue Sep 6, 2023 · 3 comments · Fixed by #104
Closed

Global regex configuration not inherited in sub-sections #64

kdeldycke opened this issue Sep 6, 2023 · 3 comments · Fixed by #104
Labels
bug Something isn't working feature-request New feature or request

Comments

@kdeldycke
Copy link
Collaborator

  • bump-my-version version: 0.10.0
  • Python version: 3.11.15
  • Operating System: macOS

Description

The no_regex parameter from the top-level section of a configuration file is not inherited in sub-sections.

What I Did

Here is a changelog.md file containing:

# Changelog

## {gh}`4.7.1 (unreleased) <compare/v4.7.0...main>`

- Rely on `bump-my-version` to update metadata.

With a pyproject.toml file containing:

[tool.bumpversion]
current_version = "4.7.1"
no_regex = true

[[tool.bumpversion.files]]
filename = "./changelog.md"
search = "{{gh}}`{current_version} (unreleased)"
replace = "{{gh}}`{new_version} (unreleased)"

Invoking the CLI, I get:

$ bump-my-version bump --dry-run --verbose patch
Starting BumpVersion 0.10.0                                                                                                                                                                                         
Reading config file pyproject.toml:                                                                                                                                                                                 
Parsing version '4.7.1' using regexp '(?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)'                                                                                                                               
Parsed the following values: major=4, minor=7, patch=1                                                                                                                                                              
Attempting to increment part 'patch'                                                                                                                                                                                
Values are now: major=4, minor=7, patch=2                                                                                                                                                                           
New version will be '4.7.2'                                                                                                                                                                                         
Dry run active, won't touch any files.                                                                                                                                                                              
Asserting files ./changelog.md contain the version string...                                                                                                                                                        
Found 're.compile('4\\.7\\.1')' in ./changelog.md at line 3: 4.7.1                                                                                                                                                  
Would not change file ./changelog.md                                                                                                                                                                                
Would write to config file pyproject.toml:                                                                                                                                                                          
*** before pyproject.toml                                                                                                                                                                                           
--- after pyproject.toml                                                                                                                                                                                            
***************                                                                                                                                                                                                     
*** 1,5 ****                                                                                                                                                                                                        
  [tool.bumpversion]                                                                                                                                                                                                
! current_version = "4.7.1"                                                                                                                                                                                         
  no_regex = true                                                                                                                                                                                                   
                                                                                                                                                                                                                    
  [[tool.bumpversion.files]]                                                                                                                                                                                        
--- 1,5 ----                                                                                                                                                                                                        
  [tool.bumpversion]                                                                                                                                                                                                
! current_version = "4.7.2"                                                                                                                                                                                         
  no_regex = true                                                                                                                                                                                                   
                                                                                                                                                                                                                    
  [[tool.bumpversion.files]]   

Here you can see the ./changelog.md file is left untouched. But I expect instead to have its version updated.

Solution

To fix this behaviour, I had to duplicate the no_regex = true parameter from the top-level [tool.bumpversion] section to the [[tool.bumpversion.files]].

So with the following pyproject.toml file:

[tool.bumpversion]
current_version = "4.7.1"
no_regex = true

[[tool.bumpversion.files]]
filename = "./changelog.md"
no_regex = true
search = "{{gh}}`{current_version} (unreleased)"
replace = "{{gh}}`{new_version} (unreleased)"

I now get the behavior I was looking for:

$ bump-my-version bump --dry-run --verbose patch
Starting BumpVersion 0.10.0                                                                                                                                                                                         
Reading config file pyproject.toml:                                                                                                                                                                                 
Parsing version '4.7.1' using regexp '(?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)'                                                                                                                               
Parsed the following values: major=4, minor=7, patch=1                                                                                                                                                              
Attempting to increment part 'patch'                                                                                                                                                                                
Values are now: major=4, minor=7, patch=2                                                                                                                                                                           
New version will be '4.7.2'                                                                                                                                                                                         
Dry run active, won't touch any files.                                                                                                                                                                              
Asserting files ./changelog.md contain the version string...                                                                                                                                                        
Found 're.compile('\\{gh\\}`4\\.7\\.1\\ \\(unreleased\\)', re.MULTILINE|re.DOTALL)' in ./changelog.md at line 3: {gh}`4.7.1 (unreleased)                                                                            
Would change file ./changelog.md:                                                                                                                                                                                   
*** before ./changelog.md                                                                                                                                                                                           
--- after ./changelog.md                                                                                                                                                                                            
***************                                                                                                                                                                                                     
*** 1,5 ****                                                                                                                                                                                                        
  # Changelog                                                                                                                                                                                                       
                                                                                                                                                                                                                    
! ## {gh}`4.7.1 (unreleased) <compare/v4.7.0...main>`                                                                                                                                                               
                                                                                                                                                                                                                    
  - Rely on `bump-my-version` to update metadata.                                                                                                                                                                   
--- 1,5 ----                                                                                                                                                                                                        
  # Changelog                                                                                                                                                                                                       
                                                                                                                                                                                                                    
! ## {gh}`4.7.2 (unreleased) <compare/v4.7.0...main>`                                                                                                                                                               
                                                                                                                                                                                                                    
  - Rely on `bump-my-version` to update metadata.                                                                                                                                                                   
Would write to config file pyproject.toml:                                                                                                                                                                          
*** before pyproject.toml                                                                                                                                                                                           
--- after pyproject.toml                                                                                                                                                                                            
***************                                                                                                                                                                                                     
*** 1,5 ****                                                                                                                                                                                                        
  [tool.bumpversion]                                                                                                                                                                                                
! current_version = "4.7.1"                                                                                                                                                                                         
  no_regex = true                                                                                                                                                                                                   
                                                                                                                                                                                                                    
  [[tool.bumpversion.files]]                                                                                                                                                                                        
--- 1,5 ----                                                                                                                                                                                                        
  [tool.bumpversion]                                                                                                                                                                                                
! current_version = "4.7.2"                                                                                                                                                                                         
  no_regex = true                                                                                                                                                                                                   
                                                                                                                                                                                                                    
  [[tool.bumpversion.files]]                                                                                                                                                                                        

Discussion

This demonstrate that I was expecting the no_regex parameter from the top-level to be inherited by default in the sub-sections.

I think this is the behavior that should be implemented. In the same vein as #59 (in which we discuss defaulting to no-regex mode), I think this behavior will reduce surprises for newcomers, make configuration files more readable, and increase the overall user-friendlyness of bump-my-version.

kdeldycke added a commit to kdeldycke/click-extra that referenced this issue Sep 6, 2023
@kdeldycke kdeldycke added bug Something isn't working feature-request New feature or request labels Sep 6, 2023
@coordt
Copy link
Member

coordt commented Nov 4, 2023

@kdeldycke Could you test this with version 0.12?

@kdeldycke
Copy link
Collaborator Author

@coordt bump-my-version 0.12.0 does not fix this issue.

Here is the proof:

Test case

A citation.cff file containing:

cff-version: 1.2.0
date-released: 2023-09-19

And given a pyproject.toml containing:

[tool.bumpversion]
current_version = "4.7.1"
regex = true

[[tool.bumpversion.files]]
filename = "./citation.cff"
search = "date-released: \\d{{4}}-\\d{{2}}-\\d{{2}}"
replace = "date-released: {utcnow:%Y-%m-%d}"

The last sub-section does not inherits the regex setting, as the following command fails:

$ bump-my-version bump --dry-run --verbose patch
Starting BumpVersion 0.12.0                                                                                                                                                                                         
Reading config file pyproject.toml:                                                                                                                                                                                 
Parsing version '4.7.1' using regexp '(?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)'                                                                                                                               
Parsed the following values: major=4, minor=7, patch=1                                                                                                                                                              
Attempting to increment part 'patch'                                                                                                                                                                                
Values are now: major=4, minor=7, patch=2                                                                                                                                                                           
New version will be '4.7.2'                                                                                                                                                                                         
Dry run active, won't touch any files.                                                                                                                                                                              
Asserting files ./citation.cff contain the version string...                                                                                                                                                        
                                                                                                                                                                                                                    
 Usage: bump-my-version bump [OPTIONS] [ARGS]...                                                                                                                                                                    
                                                                                                                                                                                                                    
 Try 'bump-my-version bump -h' for help                                                                                                                                                                             
╭─ Error ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ Did not find 'date-released: \d{4}-\d{2}-\d{2}' in file: './citation.cff'                                                                                                                                        │
╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
  

Solution

Now if we explicitely set the regex setting in the [[tool.bumpversion.files]] section:

[tool.bumpversion]
current_version = "4.7.1"
regex = true

[[tool.bumpversion.files]]
filename = "./citation.cff"
regex = true
search = "date-released: \\d{{4}}-\\d{{2}}-\\d{{2}}"
replace = "date-released: {utcnow:%Y-%m-%d}"

The same command works as expected:

$ bump-my-version bump --dry-run --verbose patch
Starting BumpVersion 0.12.0                                                                                                                                                                                         
Reading config file pyproject.toml:                                                                                                                                                                                 
Parsing version '4.7.1' using regexp '(?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)'                                                                                                                               
Parsed the following values: major=4, minor=7, patch=1                                                                                                                                                              
Attempting to increment part 'patch'                                                                                                                                                                                
Values are now: major=4, minor=7, patch=2                                                                                                                                                                           
New version will be '4.7.2'                                                                                                                                                                                         
Dry run active, won't touch any files.                                                                                                                                                                              
Asserting files ./citation.cff contain the version string...                                                                                                                                                        
Found 're.compile('date-released: \\d{4}-\\d{2}-\\d{2}', re.MULTILINE|re.DOTALL)' in ./citation.cff at line 2: date-released: 2023-09-19                                                                            
Would change file ./citation.cff:                                                                                                                                                                                   
*** before ./citation.cff                                                                                                                                                                                           
--- after ./citation.cff                                                                                                                                                                                            
***************                                                                                                                                                                                                     
*** 1,2 ****                                                                                                                                                                                                        
  cff-version: 1.2.0                                                                                                                                                                                                
! date-released: 2023-09-19                                                                                                                                                                                         
--- 1,2 ----                                                                                                                                                                                                        
  cff-version: 1.2.0                                                                                                                                                                                                
! date-released: 2023-11-05                                                                                                                                                                                         
Would write to config file pyproject.toml:                                                                                                                                                                          
*** before pyproject.toml                                                                                                                                                                                           
--- after pyproject.toml                                                                                                                                                                                            
***************                                                                                                                                                                                                     
*** 1,5 ****                                                                                                                                                                                                        
  [tool.bumpversion]                                                                                                                                                                                                
! current_version = "4.7.1"                                                                                                                                                                                         
  regex = true                                                                                                                                                                                                      
                                                                                                                                                                                                                    
  [[tool.bumpversion.files]]                                                                                                                                                                                        
--- 1,5 ----                                                                                                                                                                                                        
  [tool.bumpversion]                                                                                                                                                                                                
! current_version = "4.7.2"                                                                                                                                                                                         
  regex = true                                                                                                                                                                                                      
                                                                                                                                                                                                                    
  [[tool.bumpversion.files]]       

@kdeldycke kdeldycke changed the title Global no_regex configuration not inherited in sub-sections Global regex configuration not inherited in sub-sections Nov 5, 2023
coordt added a commit that referenced this issue Dec 15, 2023
Fixes #64

The default value of False was overriding other values.
@coordt coordt linked a pull request Dec 15, 2023 that will close this issue
@coordt
Copy link
Member

coordt commented Dec 15, 2023

@kdeldycke Do you have time to double check this PR? I re-created your test, so I'm pretty confident it is fixed.

Otherwise I'll merge it in tomorrow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working feature-request New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants