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

toml is not required. Refactor import toml #566

Closed
Metallicow opened this issue Aug 8, 2020 · 6 comments
Closed

toml is not required. Refactor import toml #566

Metallicow opened this issue Aug 8, 2020 · 6 comments
Assignees
Milestone

Comments

@Metallicow
Copy link

toml import is not required and is throwing ImportError. This needs refactored so it throws an error when trying to access features that actually use it.

try:
    import toml
except ImportError:
    print('FAILED to import toml')
    # toml import is not required. accessing these functions that use toml will throw error.
    toml = None
    
...

def SomeFuncThatUsesTOML():
    if toml is None:
        raise ImportError('toml is not required. Try "pip install toml"')

As you can see by the code, that it can be run on the cmd as a singlefile script(like pep8.py and pycodestyle.py is) and code added afterwards which makes it undoable/redoable in an editor.

Collapsible Content - Click to expand
    def OnautoPEP8(self, event=None):
        """
        Perform autopep8 operations on the active document.
        """
        self.FormatErrorsAndWarnings()
        if not self.checkedErrorsAndWarnings:
            return

        aSTC = gMainWin.activeSTC
        fileToCheck = aSTC.filepath
        if not fileToCheck:
            wx.MessageBox(_(u'File must be saved first!'), _(u'Warning'), wx.OK)
            return

        ## print('--select=%s' % self.checkedErrorsAndWarnings)
        p = subprocess.Popen([sys.executable,        # 'python',
                              _autopep8ScriptPath,   # 'autopep8.py',
                              # '--first',
                              '--select=%s' % self.checkedErrorsAndWarnings,    # Ex: '--select=E221,E222,E223,
                              # '--ignore=%s' % self.checkedErrorsAndWarnings,  # Ex: '--ignore=E',
                              # '--show-pep8',
                              # '--max-line-length=120',
                              fileToCheck],
                              stdout=subprocess.PIPE,
                              stderr=subprocess.PIPE)
        out, err = p.communicate()
        if err:
            print(err)
            raise Exception(err)
        print(out)
        ## aSTC.BeginUndoAction()
        ## aSTC.SetTarget(0, aSTC.GetLength())
        ## aSTC.ReplaceTarget(out)
        ## aSTC.EndUndoAction()
        print('OnautoPEP8')
@hhatto
Copy link
Owner

hhatto commented Aug 8, 2020

toml package is required.
https://github.com/hhatto/autopep8/blob/master/setup.py#L13

please more information.

## Your Environment
* Python version:
* autopep8 version:
* Platform: windows, linux, macOSX, and other OS...

@Metallicow
Copy link
Author

@hhatto My environment doesn't matter. I'm running autopep8.py just fine as a script. I posted code. It didn't used to fail on an import. Commenting the toml line out works for me, but it should be handled properly on your end so as to not throw an error for anyone else using it as a script from commandline without toml installed.

But since you are curious.
Py27.18 - Py3.8.x
Windows/Linux/Mac
Latest version of autopep8 or one from pypi. Im only using the script.
Basically any environment that python runs on...

@Metallicow
Copy link
Author

I just copy the autopep8.py file and the pycodestyle.py file into my project and use it/call it from there. I dont use pip for installing this. I manually extract those 2 specific files into my project from the whl/zip archives when I upgrade them from time to time.

@Metallicow
Copy link
Author

@hhatto if you comment out import toml line and run this, you will indeed find out that toml is not required and hasnt ever been. to perform the fixes on a file.

        p = subprocess.Popen([sys.executable,        # 'python',
                              _autopep8ScriptPath,   # 'autopep8.py',
                              # '--first',
                              '--select=%s' % self.checkedErrorsAndWarnings,    # Ex: '--select=E221,E222,E223,
                              # '--ignore=%s' % self.checkedErrorsAndWarnings,  # Ex: '--ignore=E',
                              # '--show-pep8',
                              # '--max-line-length=120',
                              fileToCheck],
                              stdout=subprocess.PIPE,
                              stderr=subprocess.PIPE)
        out, err = p.communicate()

@hhatto
Copy link
Owner

hhatto commented Aug 9, 2020

fix in latest master. 42aaa2a

This change will be released in version 1.5.5.

Thanks

@hhatto hhatto closed this as completed Aug 9, 2020
@hhatto hhatto self-assigned this Aug 9, 2020
@Metallicow
Copy link
Author

Yep that lazy import in the function will work fine. Thanks. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants