Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 18494f7

Browse files
authored
Added Bu Sun's updates, fixed some lint errors (GoogleCloudPlatform#1770)
* Added Bu Sun's updates, fixed some lint errors * Changes to use new nox version * Minor formatting to force a presubmit check to run * Ignore noxfile.py for tests * Clean up layout for lint
1 parent 820c29a commit 18494f7

File tree

10 files changed

+54
-38
lines changed

10 files changed

+54
-38
lines changed

.kokoro/common.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ build_file: "python-docs-samples/.kokoro/trampoline.sh"
99
# Use the Python worker docker image.
1010
env_vars: {
1111
key: "TRAMPOLINE_IMAGE"
12-
value: "gcr.io/cloud-devrel-kokoro-resources/python@sha256:d9de5f9279bccbfd75f52a179362da8fbd0ec2b08825415e793fec1c7b52bc9e"
12+
value: "gcr.io/cloud-devrel-kokoro-resources/python@sha256:4b6ba8c199e96248980db4538065cddeea594138b9b9fb2d0388603922087747"
1313
}
1414

1515
# Specify project ID

.travis.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
sudo: false
22
language: python
3+
python:
4+
- "3.6"
35
services:
46
- memcached
57
- mysql
@@ -27,7 +29,7 @@ addons:
2729
- python3.6-dev
2830
install:
2931
- pip install --upgrade pip wheel virtualenv
30-
- pip install --upgrade nox-automation
32+
- pip install --upgrade nox
3133
- pip install --upgrade git+https://github.com/dhermes/ci-diff-helper.git
3234
script:
33-
- "./scripts/travis.sh"
35+
- "./scripts/travis.sh"

appengine/standard_python37/django/mysite/settings.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,9 @@
8080
# https://docs.djangoproject.com/en/2.1/ref/settings/#databases
8181

8282
# Install PyMySQL as mysqlclient/MySQLdb to use Django's mysqlclient adapter
83-
# See https://docs.djangoproject.com/en/2.1/ref/databases/#mysql-db-api-drivers for
84-
# more information
85-
import pymysql
83+
# See https://docs.djangoproject.com/en/2.1/ref/databases/#mysql-db-api-drivers
84+
# for more information
85+
import pymysql # noqa: 402
8686
pymysql.install_as_MySQLdb()
8787

8888
# [START db_setup]
@@ -123,16 +123,16 @@
123123

124124
AUTH_PASSWORD_VALIDATORS = [
125125
{
126-
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
126+
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', # noqa: 501
127127
},
128128
{
129-
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
129+
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', # noqa: 501
130130
},
131131
{
132-
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
132+
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', # noqa: 501
133133
},
134134
{
135-
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
135+
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', # noqa: 501
136136
},
137137
]
138138

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from django.contrib import admin
22

3-
from .models import Question, Choice
3+
from .models import Choice, Question
44

55
admin.site.register(Question)
66
admin.site.register(Choice)

appengine/standard_python37/django/polls/models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class Question(models.Model):
1010

1111
def __str__(self):
1212
return self.question_text
13-
13+
1414
def was_published_recently(self):
1515
return self.pub_date >= timezone.now() - datetime.timedelta(days=1)
1616

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
from django.test import TestCase
1+
from django.test import TestCase # noqa: 401
22

33
# Create your tests here.

appengine/standard_python37/django/polls/views.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
from django.http import HttpResponse, HttpResponseRedirect
2-
from django.shortcuts import get_object_or_404 ,render
1+
from django.http import HttpResponse, HttpResponseRedirect # noqa: 401
2+
from django.shortcuts import get_object_or_404, render
33
from django.urls import reverse
44
from django.views import generic
55

@@ -24,6 +24,7 @@ class ResultsView(generic.DetailView):
2424
model = Question
2525
template_name = 'polls/results.html'
2626

27+
2728
def vote(request, question_id):
2829
question = get_object_or_404(Question, pk=question_id)
2930
try:
@@ -40,4 +41,6 @@ def vote(request, question_id):
4041
# Always return an HttpResponseRedirect after successfully dealing
4142
# with POST data. This prevents data from being posted twice if a
4243
# user hits the Back button.
43-
return HttpResponseRedirect(reverse('polls:results', args=(question.id,)))
44+
return HttpResponseRedirect(
45+
reverse('polls:results', args=(question.id,))
46+
)

bigtable/hello/main.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ def main(project_id, instance_id, table_id):
4343
# [START creating_a_table]
4444
print('Creating the {} table.'.format(table_id))
4545
table = instance.table(table_id)
46+
4647
print('Creating column family cf1 with Max Version GC rule...')
4748
# Create a column family with GC policy : most recent N versions
4849
# Define the GC policy to retain only the most recent 2 versions

ml_engine/online_prediction/predict.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ def main(project, model, version=None, force_tfrecord=False):
141141
"""Send user input to the prediction service."""
142142
while True:
143143
try:
144-
user_input = json.loads(raw_input("Valid JSON >>>"))
144+
user_input = json.loads(input("Valid JSON >>>"))
145145
except KeyboardInterrupt:
146146
return
147147

nox.py renamed to noxfile.py

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def _list_files(folder, pattern):
4040

4141
def _collect_dirs(
4242
start_dir,
43-
blacklist=set(['conftest.py', 'nox.py', 'lib', 'third_party']),
43+
blacklist=set(['conftest.py', 'noxfile.py', 'lib', 'third_party']),
4444
suffix='_test.py',
4545
recurse_further=False):
4646
"""Recursively collects a list of dirs that contain a file matching the
@@ -52,8 +52,10 @@ def _collect_dirs(
5252
# Collect all the directories that have tests in them.
5353
for parent, subdirs, files in os.walk(start_dir):
5454
if './.' in parent:
55-
continue # Skip top-level dotfiles
56-
elif any(f for f in files if f.endswith(suffix) and f not in blacklist):
55+
continue # Skip top-level dotfiles
56+
elif any(
57+
f for f in files if f.endswith(suffix) and f not in blacklist
58+
):
5759
# Don't recurse further for tests, since py.test will do that.
5860
if not recurse_further:
5961
del subdirs[:]
@@ -147,13 +149,17 @@ def _setup_appengine_sdk(session):
147149
'--show-source', '--builtin', 'gettext', '--max-complexity', '20',
148150
'--import-order-style', 'google',
149151
'--exclude', '.nox,.cache,env,lib,generated_pb2,*_pb2.py,*_pb2_grpc.py',
150-
'--ignore=E121,E123,E126,E226,E24,E704,W503,W504,I202',
152+
'--ignore=E121,E123,E126,E226,E24,E704,W503,W504,I100,I201,I202',
151153
]
152154

153155

154156
# Collect sample directories.
155157
ALL_TESTED_SAMPLES = sorted(list(_collect_dirs('.')))
156-
ALL_SAMPLE_DIRECTORIES = sorted(list(_collect_dirs('.', suffix='.py', recurse_further=True)))
158+
ALL_SAMPLE_DIRECTORIES = sorted(list(_collect_dirs(
159+
'.',
160+
suffix='.py',
161+
recurse_further=True
162+
)))
157163
GAE_STANDARD_SAMPLES = [
158164
sample for sample in ALL_TESTED_SAMPLES
159165
if sample.startswith('./appengine/standard/')]
@@ -162,9 +168,11 @@ def _setup_appengine_sdk(session):
162168
sample for sample in ALL_TESTED_SAMPLES
163169
if (sample.startswith('./appengine/standard_python37')
164170
or sample.startswith('./functions/'))]
165-
NON_GAE_STANDARD_SAMPLES_PY2 = sorted(
166-
list((set(ALL_TESTED_SAMPLES) - set(GAE_STANDARD_SAMPLES)) -
167-
set(PY3_ONLY_SAMPLES)))
171+
NON_GAE_STANDARD_SAMPLES_PY2 = sorted(list((
172+
set(ALL_TESTED_SAMPLES) -
173+
set(GAE_STANDARD_SAMPLES)) -
174+
set(PY3_ONLY_SAMPLES)
175+
))
168176
NON_GAE_STANDARD_SAMPLES_PY3 = sorted(
169177
list(set(ALL_TESTED_SAMPLES) - set(PY2_ONLY_SAMPLES)))
170178

@@ -193,7 +201,7 @@ def _session_tests(session, sample, post_install=None):
193201

194202
session.chdir(sample)
195203

196-
if os.path.exists(os.path.join(sample, 'requirements.txt')):
204+
if os.path.exists('requirements.txt'):
197205
session.install('-r', 'requirements.txt')
198206

199207
if post_install:
@@ -208,10 +216,10 @@ def _session_tests(session, sample, post_install=None):
208216
success_codes=[0, 5])
209217

210218

219+
@nox.session(python='2.7')
211220
@nox.parametrize('sample', GAE_STANDARD_SAMPLES)
212-
def session_gae(session, sample):
221+
def gae(session, sample):
213222
"""Runs py.test for an App Engine standard sample."""
214-
session.interpreter = 'python2.7'
215223

216224
# Create a lib directory if needed, otherwise the App Engine vendor library
217225
# will complain.
@@ -221,22 +229,23 @@ def session_gae(session, sample):
221229
_session_tests(session, sample, _setup_appengine_sdk)
222230

223231

232+
@nox.session(python='2.7')
224233
@nox.parametrize('sample', NON_GAE_STANDARD_SAMPLES_PY2)
225-
def session_py27(session, sample):
234+
def py27(session, sample):
226235
"""Runs py.test for a sample using Python 2.7"""
227-
session.interpreter = 'python2.7'
228236
_session_tests(session, sample)
229237

230238

239+
@nox.session(python='3.6')
231240
@nox.parametrize('sample', NON_GAE_STANDARD_SAMPLES_PY3)
232-
def session_py36(session, sample):
241+
def py36(session, sample):
233242
"""Runs py.test for a sample using Python 3.6"""
234-
session.interpreter = 'python3.6'
235243
_session_tests(session, sample)
236244

237245

246+
@nox.session
238247
@nox.parametrize('sample', ALL_SAMPLE_DIRECTORIES)
239-
def session_lint(session, sample):
248+
def lint(session, sample):
240249
"""Runs flake8 on the sample."""
241250
session.install('flake8', 'flake8-import-order')
242251

@@ -253,10 +262,9 @@ def session_lint(session, sample):
253262
# Utility sessions
254263
#
255264

256-
257-
def session_missing_tests(session):
265+
@nox.session
266+
def missing_tests(session):
258267
"""Lists all sample directories that do not have tests."""
259-
session.virtualenv = False
260268
print('The following samples do not have tests:')
261269
for sample in set(ALL_SAMPLE_DIRECTORIES) - set(ALL_TESTED_SAMPLES):
262270
print('* {}'.format(sample))
@@ -266,8 +274,9 @@ def session_missing_tests(session):
266274
list(_collect_dirs('.', suffix='.rst.in')))
267275

268276

277+
@nox.session
269278
@nox.parametrize('sample', SAMPLES_WITH_GENERATED_READMES)
270-
def session_readmegen(session, sample):
279+
def readmegen(session, sample):
271280
"""(Re-)generates the readme for a sample."""
272281
session.install('jinja2', 'pyyaml')
273282

@@ -278,7 +287,8 @@ def session_readmegen(session, sample):
278287
session.run('python', 'scripts/readme-gen/readme_gen.py', in_file)
279288

280289

281-
def session_check_requirements(session):
290+
@nox.session
291+
def check_requirements(session):
282292
"""Checks for out of date requirements and optionally updates them.
283293
284294
This is intentionally not parametric, as it's desired to never have two

0 commit comments

Comments
 (0)